您的位置:首页 > 其它

自动产生图片

2006-10-10 17:04 127 查看
一个动态生成各种格式图片的小程序,下面给出了生成jpg图片的代码,要生成其它格式,只需更改图片格式即可,代码如下:

using System.Data;
using System.Configuration;
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.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
{

protected void Button1_Click(object sender, EventArgs e)
{
Bitmap newBitmap = null;
Graphics g = null;
try
{
Font fontCounter = new Font("华文彩云", 16);

// calculate size of the string.
newBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
SizeF stringSize = g.MeasureString(TextBox1.Text , fontCounter);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
g.Dispose();
newBitmap.Dispose();

newBitmap = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.Blue ),
new Rectangle(0, 0, nWidth, nHeight));

g.DrawString(TextBox1.Text, fontCounter,
new SolidBrush(Color.Yellow ), 0, 0);

newBitmap.Save(TextBox2.Text, ImageFormat.Jpeg);
}
finally
{
if (null != g) g.Dispose();
if (null != newBitmap) newBitmap.Dispose();
}
}
}

操作界面如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: