c# 图片加上边框

 
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();


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值