绘制并填充一个简单的矩形

use*_*119 1 c# asp.net drawing

System.Drawing该怎么用呢?

我只是希望能够指定尺寸和背景颜色(十六进制),最终得到一个Image.

我看过类似的问题(比如这个)但是它们更适合WinForms - 我需要它用于ASP.Net.

Jos*_*osh 6

你可以这样做:

<%@ Page Language="C#" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>

<%

Response.Clear();
int height = 100;
int width = 200;
Random r = new Random();
int x = r.Next(75);

Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);

g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.Orange);
g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
g.DrawRectangle(Pens.Gray, 2, 2, width-3, height-3);
g.DrawRectangle(Pens.Black, 0, 0, width, height);
g.DrawString("The Code Project", new Font("Arial", 12, FontStyle.Italic), 
SystemBrushes.WindowText, new PointF(x,50) );

bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
Response.End();

%>
Run Code Online (Sandbox Code Playgroud)

这是完整文章的链接:http: //www.codeproject.com/KB/aspnet/aspnet_web_graphics.aspx