GDI画图代码 asp.net 粘贴个案例 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // Text2Image.aspx// Text2Image.aspx.csusing System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; namespace BrawDraw.Com.Utility{public partial class Utility_Text2Image : System.Web.UI.Page{int _width = 480; public int Width{get{return _width; }set{_width = value; }}int _height = 66; public int Height{get{return _height; }set{_height = value; }}string _text = string.Empty; public string Text{get{return _text; }set{_text = value; }}string _fontName = "宋体"; public string FontName{get{return _fontName; }set{_fontName = value; }}Color _fontColor = Color.Black; public Color FontColor{get{return _fontColor; }set{_fontColor = value; }}Color _backgroundColor = Color.White; public Color BackgroundColor{get{return _backgroundColor; }set{_backgroundColor = value; }}//华文新魏protected void Page_Load(object sender, EventArgs e){if (Request.QueryString["w"] != null){try{this._width = int.Parse(Request.QueryString["w"].Trim()); }finally{}}if (Request.QueryString["h"] != null){try{this._height = int.Parse(Request.QueryString["h"].Trim()); }finally{}}if (Request.QueryString["text"] != null){this._text = Request.QueryString["text"].Trim(); }else if (Request.QueryString["t"] != null){this._text = Request.QueryString["t"].Trim(); }if (Request.QueryString["font"] != null){this._fontName = Request.QueryString["font"].Trim(); }else if (Request.QueryString["f"] != null){this._fontName = Request.QueryString["f"].Trim(); }if (this._fontName == "大黑"){this._fontName = "方正大黑简体"; }string colorString = "Black"; if (Request.QueryString["color"] != null){colorString = Request.QueryString["color"].Trim(); }else if (Request.QueryString["c"] != null){colorString = Request.QueryString["c"].Trim(); }if (colorString.StartsWith("_")){colorString = "#" + colorString.Remove(0, 1); }this._fontColor = ConvertColor(colorString); string bgColorString = "White"; if (Request.QueryString["bgcolor"] != null){bgColorString = Request.QueryString["bgcolor"].Trim(); }else if (Request.QueryString["b"] != null){bgColorString = Request.QueryString["b"].Trim(); }if (bgColorString.StartsWith("_")){bgColorString = "#" + bgColorString.Remove(0, 1); }this._backgroundColor = ConvertColor(bgColorString); if (!IsPostBack){CreateImage(this.Text, this.Width, this.Height, this.FontName, this.FontColor, this.BackgroundColor); }}//根据验证字符串生成最终图象public void CreateImage(string text, int width, int height, string fontName, Color fontColor, Color backgroundColor){Bitmap theBitmap = new Bitmap(width, height); Graphics theGraphics = Graphics.FromImage(theBitmap); theGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; theGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; theGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // 背景theGraphics.Clear(backgroundColor); GraphicsPath gp = new GraphicsPath(); FontFamily fontFamily; try{fontFamily = new FontFamily(fontName); }catch (Exception exc){fontFamily = new FontFamily("宋体"); }StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Near; format.LineAlignment = StringAlignment.Center; gp.AddString(text, fontFamily, 0, 60f, new Point(0, 0), format); RectangleF rectF = gp.GetBounds(); float scaleX = width * 1.0f / rectF.Width; float scaleY = height * 1.0f / rectF.Height; float offsetX = rectF.X * scaleX; float offsetY = rectF.Y * scaleY; System.Drawing.Drawing2D.Matrix matrix = new Matrix(scaleX, 0, 0, scaleY, -offsetX, -offsetY); gp.Transform(matrix); Brush newBrush = new SolidBrush(fontColor); theGraphics.FillPath(newBrush, gp); //theGraphics.DrawRectangle(Pens.Black, 0, 0, width-2, height-2); if (gp != null) gp.Dispose(); // 将生成的图片发回客户端MemoryStream ms = new MemoryStream(); theBitmap.Save(ms, ImageFormat.Png); Response.ClearContent(); //需要输出图象信息 要修改HTTP头 Response.ContentType = "image/Png"; Response.BinaryWrite(ms.ToArray()); theGraphics.Dispose(); theBitmap.Dispose(); Response.End(); }private Color ConvertColor(string colorString){Array knownColors = System.Enum.GetValues(typeof(KnownColor)); foreach (object colorName in knownColors){if (colorString.ToLower() == colorName.ToString().ToLower()){return Color.FromName(colorString); }}if (colorString.StartsWith("#")){return ColorTranslator.FromHtml(colorString); }try{int color32argb = int.Parse(colorString); return Color.FromArgb(color32argb); }finally{ }return Color.Black; }}} System.Drawing.Image MyImage=new System.Drawing.Bitmap (w,h);//申请位图对象System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(MyImage); //申请画图对象//用白色C清出除Color C=Color.FromArgb(255,255,255);g.Clear(C); //画线g.DrawLine(Pens.Black,x1,y1,x2,y2);//画一个象素的点//MyBitmap.SetPixel(x, y, Color.White);g.DrawImage(MyImage,0,0,h,w); //pictureBox1在(0,0)到(h,w)范围画点pictureBox1.Image=MyImage; //这个图片贴到pictureBox1控件上2.在窗体上绘制图形System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷System.Drawing.Graphics formGraphics = this.CreateGraphics();formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形 //画多边形 formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point(200,170), new Point(70,350), new Point(50,200)}); //清理使用的资源myPen.Dispose();myBrush.Dispose();formGraphics.Dispose();3.在窗体上绘制文本//在窗体上绘制竖向文本 System.Drawing.Graphics formGraphics = this.CreateGraphics();string drawString = "Text";System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);float x = 150.0f;float y = 50.0f;System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本垂直formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本//清理使用的资源drawFont.Dispose();drawBrush.Dispose();formGraphics.Dispose();4.其他画笔//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格 HatchStyle hs = HatchStyle.Cross; //十字HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red); g.FillRectangle(sb,50,50,150,150); Rectangle r = new Rectangle(500, 300, 100, 100); LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); g.FillRectangle(lb, r); //PathGradientBrush路径渐变,LinearGradientBrush线性渐变Image bgimage = new Bitmap("E:2065215396.jpg"); brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景 g.FillEllipse(brush,50,50,500,300);5.其他技巧//申请画图区域System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);//创建笔System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格//创建单色画刷System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);//画图函数protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e ) {}//颜色对话框ColorDialog c = new ColorDialog(); c.ShowDialog(); textBox1.BackColor = c.Color; //字体对话框FontDialog f = new FontDialog(); f.ShowDialog(); textBox1.Font = flg.Font; //RGB的使用int Red=(int)SelfPlaceKey.GetValue("Red");int Green=(int)SelfPlaceKey.GetValue("Green");int Blue=(int)SelfPlaceKey.GetValue("Blue");BackColor=Color.FromArgb(Red,Green,Blue);//字体Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);rect=new Rectangle(0,y,400,aFont.Height);g.DrawRectangle(Pens.Blue,rect);StringFormat sf=new StringFormat();sf.Alignment=StringAlignment.Far;g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);y+=aFont.Height+20;aFont.Dispose(); 这个循环里的正则表达式的局部变量,如何写到循环外面去,以减少内存消耗? WPF中StoryBoard的问题 ● 谁能把这个【flash】调用的图片取出来??? 关于COM+的问题!高手帮忙! csv转DataSet发现奇怪问题 高手来看看阿!!!!!!! 获取ListView滚动条事件,另外看看ListView的Bug! 高分求解,怎样处理Html格式文件 System.Threading.Timer中show窗体,假死 Regex.IsMatch(str,@"^\d+$") 关于调用C++ DLL 结构体参数 C#不支持函数参数的默认值?那么相关的功能如何实现呢 小菜求解c#程序问题
// Text2Image.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO; namespace BrawDraw.Com.Utility
{
public partial class Utility_Text2Image : System.Web.UI.Page
{
int _width = 480;
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}int _height = 66;
public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
}string _text = string.Empty;
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}string _fontName = "宋体";
public string FontName
{
get
{
return _fontName;
}
set
{
_fontName = value;
}
}Color _fontColor = Color.Black;
public Color FontColor
{
get
{
return _fontColor;
}
set
{
_fontColor = value;
}
}Color _backgroundColor = Color.White;
public Color BackgroundColor
{
get
{
return _backgroundColor;
}
set
{
_backgroundColor = value;
}
}//华文新魏
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["w"] != null)
{
try
{
this._width = int.Parse(Request.QueryString["w"].Trim());
}
finally
{
}
}if (Request.QueryString["h"] != null)
{
try
{
this._height = int.Parse(Request.QueryString["h"].Trim());
}
finally
{
}
}if (Request.QueryString["text"] != null)
{
this._text = Request.QueryString["text"].Trim();
}
else if (Request.QueryString["t"] != null)
{
this._text = Request.QueryString["t"].Trim();
}if (Request.QueryString["font"] != null)
{
this._fontName = Request.QueryString["font"].Trim();
}
else if (Request.QueryString["f"] != null)
{
this._fontName = Request.QueryString["f"].Trim();
}
if (this._fontName == "大黑")
{
this._fontName = "方正大黑简体";
}
string colorString = "Black";
if (Request.QueryString["color"] != null)
{
colorString = Request.QueryString["color"].Trim();
}
else if (Request.QueryString["c"] != null)
{
colorString = Request.QueryString["c"].Trim();
}
if (colorString.StartsWith("_"))
{
colorString = "#" + colorString.Remove(0, 1);
}
this._fontColor = ConvertColor(colorString); string bgColorString = "White";
if (Request.QueryString["bgcolor"] != null)
{
bgColorString = Request.QueryString["bgcolor"].Trim();
}
else if (Request.QueryString["b"] != null)
{
bgColorString = Request.QueryString["b"].Trim();
}
if (bgColorString.StartsWith("_"))
{
bgColorString = "#" + bgColorString.Remove(0, 1);
}this._backgroundColor = ConvertColor(bgColorString); if (!IsPostBack)
{
CreateImage(this.Text, this.Width, this.Height, this.FontName, this.FontColor, this.BackgroundColor);
}
}//根据验证字符串生成最终图象
public void CreateImage(string text, int width, int height, string fontName, Color fontColor, Color backgroundColor)
{
Bitmap theBitmap = new Bitmap(width, height);
Graphics theGraphics = Graphics.FromImage(theBitmap);
theGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
theGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
theGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// 背景
theGraphics.Clear(backgroundColor); GraphicsPath gp = new GraphicsPath();
FontFamily fontFamily;
try
{
fontFamily = new FontFamily(fontName);
}
catch (Exception exc)
{
fontFamily = new FontFamily("宋体");
}
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center; gp.AddString(text, fontFamily, 0, 60f, new Point(0, 0), format);
RectangleF rectF = gp.GetBounds();
float scaleX = width * 1.0f / rectF.Width;
float scaleY = height * 1.0f / rectF.Height;
float offsetX = rectF.X * scaleX;
float offsetY = rectF.Y * scaleY;
System.Drawing.Drawing2D.Matrix matrix = new Matrix(scaleX, 0, 0, scaleY, -offsetX, -offsetY);
gp.Transform(matrix);
Brush newBrush = new SolidBrush(fontColor);
theGraphics.FillPath(newBrush, gp);
//theGraphics.DrawRectangle(Pens.Black, 0, 0, width-2, height-2); if (gp != null) gp.Dispose(); // 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png); Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}private Color ConvertColor(string colorString)
{
Array knownColors = System.Enum.GetValues(typeof(KnownColor));
foreach (object colorName in knownColors)
{
if (colorString.ToLower() == colorName.ToString().ToLower())
{
return Color.FromName(colorString);
}
}if (colorString.StartsWith("#"))
{
return ColorTranslator.FromHtml(colorString);
}try
{
int color32argb = int.Parse(colorString);
return Color.FromArgb(color32argb);
}
finally
{ }return Color.Black;
}
}
}
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(MyImage); //申请画图对象
//用白色C清出除
Color C=Color.FromArgb(255,255,255);
g.Clear(C);
//画线
g.DrawLine(Pens.Black,x1,y1,x2,y2);
//画一个象素的点
//MyBitmap.SetPixel(x, y, Color.White);g.DrawImage(MyImage,0,0,h,w); //pictureBox1在(0,0)到(h,w)范围画点
pictureBox1.Image=MyImage; //这个图片贴到pictureBox1控件上
2.在窗体上绘制图形
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形
formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形
//画多边形
formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point(200,170), new Point(70,350), new Point(50,200)});
//清理使用的资源
myPen.Dispose();
myBrush.Dispose();
formGraphics.Dispose();
3.在窗体上绘制文本
//在窗体上绘制竖向文本
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本//清理使用的资源
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();4.其他画笔
//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格
HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150); Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
//PathGradientBrush路径渐变,LinearGradientBrush线性渐变Image bgimage = new Bitmap("E:2065215396.jpg");
brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景
g.FillEllipse(brush,50,50,500,300);
5.其他技巧//申请画图区域
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
//创建笔
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格
//创建单色画刷
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//画图函数
protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
}//颜色对话框
ColorDialog c = new ColorDialog();
c.ShowDialog();
textBox1.BackColor = c.Color;
//字体对话框
FontDialog f = new FontDialog();
f.ShowDialog();
textBox1.Font = flg.Font; //RGB的使用
int Red=(int)SelfPlaceKey.GetValue("Red");
int Green=(int)SelfPlaceKey.GetValue("Green");
int Blue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);//字体
Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);
rect=new Rectangle(0,y,400,aFont.Height);
g.DrawRectangle(Pens.Blue,rect);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Far;
g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);
y+=aFont.Height+20;
aFont.Dispose();