ASP.NET 图像

System.Drawing.Image 对象

   
FromFile(string filename) 从文件中读入图像
Save(string filename) 写入到文件中
public void Save (
   Stream stream,
   ImageFormat format
)
写入到流中
public Image GetThumbnailImage (
int thumbWidth,
int thumbHeight,
GetThumbnailImageAbort callback,
IntPtr callbackData
)
生成缩略图

System.Drawing.Graphics 对象

   
FromImage(Image) 从指定图像生成对象
public void DrawString (
string s,
Font font,
Brush brush,
PointF point
)
在图像中输出文字
FillRectangle(...) 填充矩形区域

参照

new SolidBrush(Color.Red) 生成用于填充的刷子 Brush 对象
Color.White 获取颜色常量
new PointF(100,100) 生成代表坐标的 PointF 对象

示例: 图像数据操作.

从文件载入图像 System.Drawing.Image img = System.Drawing.Image.FromFile("C:\\1.jpg");
生成缩略图 System.Drawing.Image img2 = img.GetThumbnailImage(150, 150, null,new IntPtr() );
图像数据到流 System.IO.MemoryStream stm = new System.IO.MemoryStream();
img.Save(stm, System.Drawing.Imaging.ImageFormat.Jpeg);
流到字节 byte[] buff=new byte[stm.Length];
stm.Position = 0;
stm.Read(buff, 0, (int)stm.Length);
字节输出到网页 Response.ContentType = "image/pjpeg";
Response.BinaryWrite(buff);

示例: 随机生成图像

新建图像 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
System.Drawing.Graphics g = Graphics.FromImage(bmp);
设置背景 g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, bmp.Width, bmp.Height));
显示文字 g.DrawString("1234",new Font("宋体",9),new SolidBrush(Color.Blue),new PointF());