導入以下兩個包:
System.Drawing;
System.Drawing.Imaging;
建產對象:
Bitmap bm = new Bitmap("c:/1.bmp");
縮放:
Bitmap bm1 = new Bitmap(bm,width,height);
格式轉換:
bm.save("c:/1.jpg",ImageFromat.Jpeg);
bm1.Save("c:/1.gif", ImageFormat.Gif);
剪切一個區域:
//剪切大小
int cutwidth;
int cutheight;
Graphics g;
//以大小為剪切大小,像素格式為32位RGB創建一個位圖對像
Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;
//定義一個區域
Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);
//要繪制到的位圖
g = Graphics.FromImage(bm1);
//將bm內rg所指定的區域繪制到bm1
g.DrawImage(bm,rg)
============================================
C#Bitmap代替另一個Bitmap的某部分
Bitmap bm = new Bitmap(寬度, 高度);// 新建一個 Bitmap 位圖
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根據新建的 Bitmap 位圖,創建畫布
g.Clear(System.Drawing.Color.Black);// 使用黑色重置畫布
g.DrawImage(源位圖, ......); // 繪制“源位圖”,后面有若干參數控制大小、坐標等等功能。
Bitmap bm = new Bitmap(寬度, 高度);// 新建一個 Bitmap 位圖
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根據新建的 Bitmap 位圖,創建畫布
g.Clear(System.Drawing.Color.Black);// 使用黑色重置畫布
g.DrawImage(源位圖, ......); // 繪制“源位圖”,后面有若干參數控制大小、坐標等等功能。
==================================================
C# 圖片處理之:旋轉圖片任意角度
C# 圖片處理之:旋轉圖片任意角度
///<summary>
/// 任意角度旋轉
///</summary>
///<param name="bmp">原始圖Bitmap</param>
///<param name="angle">旋轉角度</param>
///<param name="bkColor">背景色</param>
///<returns>輸出Bitmap</returns>
publicstatic Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
{
int w = bmp.Width +2;
int h = bmp.Height +2;
PixelFormat pf;
if (bkColor == Color.Transparent)
{
pf = PixelFormat.Format32bppArgb;
}
else
{
pf = bmp.PixelFormat;
}
Bitmap tmp =new Bitmap(w, h, pf);
Graphics g = Graphics.FromImage(tmp);
g.Clear(bkColor);
g.DrawImageUnscaled(bmp, 1, 1);
g.Dispose();
GraphicsPath path =new GraphicsPath();
path.AddRectangle(new RectangleF(0f, 0f, w, h));
Matrix mtrx =new Matrix();
mtrx.Rotate(angle);
RectangleF rct = path.GetBounds(mtrx);
Bitmap dst =new Bitmap((int)rct.Width, (int)rct.Height, pf);
g = Graphics.FromImage(dst);
g.Clear(bkColor);
g.TranslateTransform(-rct.X, -rct.Y);
g.RotateTransform(angle);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImageUnscaled(tmp, 0, 0);
g.Dispose();
tmp.Dispose();
return dst;
}
-------------------------
這張圖由以下代碼產生。左面的是用DrawString函數直接繪制在Form上的,右面的是在Bitmap對象中繪制好后,DrawImage到窗體上的。可以看到文本顯示效果非常不同。因為我想使用雙緩沖,把大量文本先繪制在BitMap上,再讓它顯示出來,但是兩種顯示效果不一樣是無法容忍的。請問,這是為什么?怎樣讓兩種方法繪制的文本顯示效果一模一樣即使換了字體,兩種方法的顯示效果仍然不一致。
private void Form1_Paint(object sender, PaintEventArgs e)
{
bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics gBmp = Graphics.FromImage(bmp);//內存位圖
Graphics gForm = e.Graphics;//Form
for (int i = 1; i < 20; i++)
{
System.Drawing.Font f = new Font("宋體", i, FontStyle.Regular);
gForm.DrawString("this is a test", f, Brushes.Black, new PointF(10f, i*Font.Height));
gBmp.DrawString("this is a test", f, Brushes.Black, new PointF(0f, i * Font.Height));
}
gForm.DrawImage(bmp, new Point(200, 0));
{
bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics gBmp = Graphics.FromImage(bmp);//內存位圖
Graphics gForm = e.Graphics;//Form
for (int i = 1; i < 20; i++)
{
System.Drawing.Font f = new Font("宋體", i, FontStyle.Regular);
gForm.DrawString("this is a test", f, Brushes.Black, new PointF(10f, i*Font.Height));
gBmp.DrawString("this is a test", f, Brushes.Black, new PointF(0f, i * Font.Height));
}
gForm.DrawImage(bmp, new Point(200, 0));
