C#一些經常使用的圖片操做方法:生成文字圖片 合併圖片等

生成文字圖片:算法

 1         /// <summary>
 2         /// 生成文字圖片
 3         /// </summary>
 4         /// <param name="text"></param>
 5         /// <param name="isBold"></param>
 6         /// <param name="fontSize"></param>
 7         public Image CreateImage(string text, bool isBold, int fontSize)
 8         {
 9             int wid = 400;
10             int high = 200;
11             Font font;
12             if (isBold)
13             {
14                 font = new Font("Arial", fontSize, FontStyle.Bold);
15 
16             }
17             else
18             {
19                 font = new Font("Arial", fontSize, FontStyle.Regular);
20 
21             }
22             //繪筆顏色
23             SolidBrush brush = new SolidBrush(Color.Black);
24             StringFormat format = new StringFormat(StringFormatFlags.NoClip);
25             Bitmap image = new Bitmap(wid, high);
26             Graphics g = Graphics.FromImage(image);
27             SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//獲得文本的寬高
28             int width = (int)(sizef.Width + 1);
29             int height = (int)(sizef.Height + 1);
30             image.Dispose();
31             image = new Bitmap(width, height);
32             g = Graphics.FromImage(image);
33             g.Clear(Color.White);//透明
34 
35             RectangleF rect = new RectangleF(0, 0, width, height);
36             //繪製圖片
37             g.DrawString(text, font, brush, rect);
38             //釋放對象
39             g.Dispose();
40             return image;
41         }

合併圖片:spa

 1         /// <summary>  
 2         /// 合併圖片  
 3         /// </summary>  
 4         /// <param name="imgBack"></param>  
 5         /// <param name="img"></param>  
 6         /// <returns></returns>  
 7         public static Bitmap CombinImage(Image imgBack, Image img, int xDeviation = 0, int yDeviation = 0)
 8         {
 9 
10             Bitmap bmp = new Bitmap(imgBack.Width, imgBack.Height + img.Height);
11 
12             Graphics g = Graphics.FromImage(bmp);
13             g.Clear(Color.White);
14             g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框寬, 相框高);     
15 
16             //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一層黑色邊框    
17 
18             //g.DrawImage(img, 照片與相框的左邊距, 照片與相框的上邊距, 照片寬, 照片高);    
19 
20             g.DrawImage(img, imgBack.Width / 2 - img.Width / 2 + xDeviation, imgBack.Height + yDeviation, img.Width, img.Height);
21             GC.Collect();
22             return bmp;
23         }    
 1         /// <summary>  
 2         /// Resize圖片  
 3         /// </summary>  
 4         /// <param name="bmp">原始Bitmap</param>  
 5         /// <param name="newW">新的寬度</param>  
 6         /// <param name="newH">新的高度</param>  
 7         /// <param name="mode">保留着,暫時未用</param>  
 8         /// <returns>處理之後的圖片</returns>  
 9         public static Image ResizeImage(Image bmp, int newW, int newH, int mode)
10         {
11             try
12             {
13                 Image b = new Bitmap(newW, newH);
14                 Graphics g = Graphics.FromImage(b);
15 
16                 // 插值算法的質量    
17                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
18                 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height),
19                             GraphicsUnit.Pixel);
20                 g.Dispose();
21                 return b;
22             }
23             catch
24             {
25                 return null;
26             }
27         }    

MemoryStream保存到圖片code

1         Bitmap bmp = CombinImage(ms, img1);
2         MemoryStream ms = new MemoryStream();
3         bmp.Save(ms, ImageFormat.Png);        
相關文章
相關標籤/搜索