绘制bitmap图片保存,生成ico文件或者对象

自己绘制bitmap图片保存,生成ico文件或者对象
今天回答一个问题的时候的随笔
 
  1. Bitmap bit = new Bitmap(100, 30);
  2.             Graphics g = Graphics.FromImage(bit);
  3.             SolidBrush sb = new SolidBrush(Color.Blue);
  4.             Rectangle rg = new Rectangle(new Point(0, 0), bit.Size);
  5.             g.FillRectangle(sb, rg);
  6.             g.DrawString("测试测试呵呵"this.Font, new SolidBrush(Color.White), new PointF(0, 0));
  7.             bit.Save("d://123.bmp");//保存下来这个可以看生成的图片

 

msdn上面有详解,先将你需要生成的图片与文字合成一个image对象,然后生成icon对象,最后用Graphics 操作或者直接用icon类进行操作

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse(这是 Paint 事件处理程序的参数)。代码执行下列操作:

创建一个 Bitmap。

将该对象绘制到屏幕。

获取 Bitmap 的图标句柄。

将窗体的 Form.Icon 属性设置为从该句柄创建的图标。

调用 Win32 API 函数 DestroyIcon 以释放资源。


C# code
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=CharSet.Auto)] extern static bool DestroyIcon(IntPtr handle); private void GetHicon_Example(PaintEventArgs e) { // Create a Bitmap object from an image file. Bitmap myBitmap = new Bitmap(@"c:/FakePhoto.jpg"); // Draw myBitmap to the screen. e.Graphics.DrawImage(myBitmap, 0, 0); // Get an Hicon for myBitmap. IntPtr Hicon = myBitmap.GetHicon(); // Create a new icon from the handle. Icon newIcon = Icon.FromHandle(Hicon); // Set the form Icon attribute to the new icon. this.Icon = newIcon; // Destroy the Icon, since the form creates // its own copy of the icon. DestroyIcon(newIcon.Handle); }
posted @ 2008-09-10 15:49  Sean.Z  阅读(343)  评论(0编辑  收藏  举报