技术已荒废

生活的理想,是为了理想的生活!

---->>Nothing is impossible for a willing heart.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
一个动态生成各种格式图片的小程序,下面给出了生成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(11, 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(00, nWidth, nHeight));

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

            newBitmap.Save(TextBox2.Text, ImageFormat.Jpeg);
        }

        
finally
        
{
            
if (null != g) g.Dispose();
            
if (null != newBitmap) newBitmap.Dispose();
        }

    }

}

操作界面如下:
posted on 2006-10-10 17:04  技术已荒废  阅读(578)  评论(1编辑  收藏  举报