c# 图片加上边框

来源:互联网 发布:wing ide mac 编辑:程序博客网 时间:2024/04/30 00:52
 
Image img = Bitmap.FromFile(openFileDialog1.FileName.ToString());int bordwidth = Convert.ToInt32(img.Width * 0.1);int bordheight = Convert.ToInt32(img.Height * 0.1);int newheight = img.Height + bordheight;int newwidth = img.Width + bordwidth;Color bordcolor = Color.Black;Bitmap bmp = new Bitmap(newwidth, newheight);Graphics g = Graphics.FromImage(bmp);g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;int Style = 0;     //New: 绘制边框的类型, 手动修改0,1,2 可改变边框类型if (Style == 0)   //New: 整个边框.{    //Changed: 修改rec区域, 将原图缩放. 适合边框内    System.Drawing.Rectangle rec = new Rectangle(bordwidth / 2, bordwidth / 2, newwidth - bordwidth / 2, newheight - bordwidth / 2);    g.DrawImage(img, rec, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);    g.DrawRectangle(new Pen(bordcolor, bordheight), 0, 0, newwidth, newheight);}else if (Style == 1)   //New: 上下边框.{     System.Drawing.Rectangle rec = new Rectangle(0, bordwidth / 2, newwidth, newheight - bordwidth / 2);     g.DrawImage(img, rec, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);     g.DrawLine(new Pen(bordcolor, bordheight), 0, 0, newwidth, 0);     g.DrawLine(new Pen(bordcolor, bordheight), 0, newheight, newwidth, newheight);}else if (Style == 2)   //New: 左右边框.{     System.Drawing.Rectangle rec = new Rectangle(bordwidth / 2, 0, newwidth - bordwidth / 2, newheight);     g.DrawImage(img, rec, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);     g.DrawLine(new Pen(bordcolor, bordheight), 0, 0, 0, newheight);     g.DrawLine(new Pen(bordcolor, bordheight), newwidth, 0, newwidth, newheight);}bmp.Save(DateTime.Now.ToString("mmssfff")+".jpg");g.Dispose();

原创粉丝点击