c# 将控件保存为图片

Bitmap NewBitmap = new Bitmap(panel1.Width, panel1.Height);

                panel1.DrawToBitmap(NewBitmap, new Rectangle(0, 0,NewBitmap.Width, NewBitmap.Height));

               NewBitmap.Save(Application.StartupPath + @"..\Picture\LayOut\xx.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

     // MessageBox.Show(Application.StartupPath + @"\Picture\LayOut\xx.jpg");
            NewBitmap.Dispose();

private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack ){

Bitmap newBitmap = new Bitmap(36,16,PixelFormat.Format32bppArgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White),new Rectangle(0,0,36,16));
System.Drawing.Font textFont = new Font("Times new Roman",10);
System.Drawing.RectangleF rectangle = new RectangleF(0,0,36,16);
Random rd = new Random();
int valationNo = rd.Next(1000,8999);
g.FillRectangle(new SolidBrush(Color.BurlyWood),rectangle);
g.DrawString(valationNo.ToString(),textFont,new SolidBrush(Color.Blue),rectangle);
newBitmap.Save(Server.MapPath("img")+"\\19.gif",ImageFormat.Gif);

g.Dispose();//记得释放资源
newBitmap.Dispose();
}
}  

posted on 2009-09-27 15:58  风乔  阅读(845)  评论(0编辑  收藏  举报

导航