【.NET】GDI+位图图型操作笔记1

最近做的图型操作有点多,也有点杂,自己来整理一下。

  • 图型加载可以有文件流的,字符流的,内存流的。
  • 做的时候一些注意的地方,比如文件正在使用,未释放流的内存。
  • 下面是最最最基础的写法。

BitMap

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(320, 320);//宽320高320

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Server.MapPath("/images/1.jpg"));//加载一张图片

Graphice

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.HighQuality;

在Graphice贴上图片,画上文字

//画上图片
string oImg = "/image/2.jpg";
int x=130,y=130,width=50,height=50;
g.DrawImage(oImg, new System.Drawing.Rectangle(0, 0, 320, 320),
                new System.Drawing.Rectangle(x, y, width, hight),
                System.Drawing.GraphicsUnit.Pixel);

//画上文字
string addText="图型操作";
System.Drawing.Font font = new System.Drawing.Font(new FontFamily("微软雅黑"), 30, FontStyle.Regular, GraphicsUnit.Pixel);
System.Drawing.Brush b = new SolidBrush(ColorTranslator.FromHtml("#666666"));
g.DrawString(addText, f, b, 320 / 2 - 60, 320 / 2 - 10);

保存

string filename=Server.MapPath("aaa.jpg");
//新建目录
Directory.CreateDirectory(Path.GetDirectoryName(filename));
//保存
bitmap.Save(filename);
bitmap.Dispose();
原文地址:https://www.cnblogs.com/laokchen/p/12650043.html