導(dǎo)入以下兩個包: System.Drawing; System.Drawing.Imaging; 建產(chǎn)對象: Bitmap bm = new Bitmap("c:/1.bmp"); 縮放: Bitmap bm1 = new Bitmap(bm,width,height); 格式轉(zhuǎn)換: bm.save("c:/1.jpg",ImageFromat.Jpeg); bm1.Save("c:/1.gif", ImageFormat.Gif); 剪切一個區(qū)域: //剪切大小 int cutwidth; int cutheight; Graphics g; //以大小為剪切大小,像素格式為32位RGB創(chuàng)建一個位圖對像 Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ; //定義一個區(qū)域 Rectangle rg = new Rectangle(0,0,cutwidth,cutheight); //要繪制到的位圖 g = Graphics.FromImage(bm1); //將bm內(nèi)rg所指定的區(qū)域繪制到bm1 g.DrawImage(bm,rg) ============================================
C#Bitmap代替另一個Bitmap的某部分
Bitmap bm = new Bitmap(寬度, 高度);// 新建一個 Bitmap 位圖 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根據(jù)新建的 Bitmap 位圖,創(chuàng)建畫布 g.Clear(System.Drawing.Color.Black);// 使用黑色重置畫布 g.DrawImage(源位圖, ......); // 繪制“源位圖”,后面有若干參數(shù)控制大小、坐標(biāo)等等功能。 ==================================================
C# 圖片處理之:旋轉(zhuǎn)圖片任意角度 ///<summary>
/// 任意角度旋轉(zhuǎn) ///</summary> ///<param name="bmp">原始圖Bitmap</param> ///<param name="angle">旋轉(zhuǎn)角度</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; } -------------------------
這張圖由以下代碼產(chǎn)生。左面的是用DrawString函數(shù)直接繪制在Form上的,右面的是在Bitmap對象中繪制好后,DrawImage到窗體上的??梢钥吹轿谋撅@示效果非常不同。因?yàn)槲蚁胧褂秒p緩沖,把大量文本先繪制在BitMap上,再讓它顯示出來,但是兩種顯示效果不一樣是無法容忍的。請問,這是為什么?怎樣讓兩種方法繪制的文本顯示效果一模一樣即使換了字體,兩種方法的顯示效果仍然不一致。
private void Form1_Paint(object sender, PaintEventArgs e) { bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics gBmp = Graphics.FromImage(bmp);//內(nèi)存位圖 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)); } |
|