Graphics.FromImage()的返回值探讨
在学习P501页,Baunce.cs的程序时发现“小球”Graphics实例中在进行grfx = Graphics.FromImage(bitmap);后,要想展现在屏幕上,还是要再次创建Graphics grfx = CreateGraphics();实例,再使用grfx.DrawImage();才能在屏幕上展示出来。
下面使用另外的一个实例进行演示:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace BitmapDemo 9 { 10 class Program:Form 11 { 12 //Bitmap bitmap; 13 14 static void Main(string[] args) 15 { 16 Application.Run(new Program()); 17 } 18 public Program() 19 { 20 Text = "Bitmap Demo"; 21 //bitmap = new Bitmap(100, 100); 22 //Graphics grfx = Graphics.FromImage(bitmap); 23 24 //grfx.Clear(Color.Green); 25 //grfx.DrawString("chenlight", Font, Brushes.Red, 0, 0); 26 27 } 28 protected override void OnPaint(PaintEventArgs e) 29 { 30 //Graphics g = e.Graphics; 31 32 //bitmap = new Bitmap(100, 100); 33 //Graphics grfx = Graphics.FromImage(bitmap); 34 35 //grfx.Clear(Color.Green); 36 //grfx.DrawString("chenlight", Font, Brushes.Red, 0, 0); 37 //grfx.DrawEllipse(new Pen(Color.Red), 0, 0, 100, 100); 38 39 //g.DrawImage(bitmap, 5, 5); 40 41 42 // 此代码示例演示如何在 C# 中以编程方式创建新位图。 43 // 初始化位图对象 44 Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 45 46 // 创建一个新图形 47 Graphics graphics = Graphics.FromImage(bitmap); 48 49 // 初始化笔 50 Pen pen = new Pen(Color.Red, 5); 51 52 // 画一个矩形 53 graphics.DrawEllipse(pen, new Rectangle(0, 0, 700, 700)); 54 55 // 保存文件 56 bitmap.Save(@"C:\output.png"); 57 58 } 59 } 60 }
虽然没有在屏幕上显示出来,但是在生成的out.png图像文件中圆形是“画”在图像中了,如下
下面换一种代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace BitmapDemo 9 { 10 class Program:Form 11 { 12 Bitmap bitmap; 13 14 static void Main(string[] args) 15 { 16 Application.Run(new Program()); 17 } 18 public Program() 19 { 20 Text = "Bitmap Demo"; 21 //bitmap = new Bitmap(100, 100); 22 //Graphics grfx = Graphics.FromImage(bitmap); 23 24 //grfx.Clear(Color.Green); 25 //grfx.DrawString("chenlight", Font, Brushes.Red, 0, 0); 26 27 } 28 protected override void OnPaint(PaintEventArgs e) 29 { 30 Graphics g = e.Graphics; 31 32 bitmap = new Bitmap(100, 100); 33 Graphics grfx = Graphics.FromImage(bitmap); 34 35 grfx.Clear(Color.Green); 36 grfx.DrawString("chenlight", Font, Brushes.Red, 0, 0); 37 grfx.DrawEllipse(new Pen(Color.Red), 0, 0, 100, 100); 38 39 g.DrawImage(bitmap, 5, 5); 40 41 42 // 此代码示例演示如何在 C# 中以编程方式创建新位图。 43 // 初始化位图对象 44 //Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 45 46 //// 创建一个新图形 47 //Graphics graphics = Graphics.FromImage(bitmap); 48 49 //// 初始化笔 50 //Pen pen = new Pen(Color.Red, 5); 51 52 //// 画一个矩形 53 //graphics.DrawEllipse(pen, new Rectangle(0, 0, 700, 700)); 54 55 //// 保存文件 56 //bitmap.Save(@"C:\output.png"); 57 58 } 59 } 60 }
这次可以在屏幕上正常展示:绿色背景、文字、椭圆;
通过.Net Reflector分析一下
1 public static Graphics FromImage(Image image) 2 { 3 if (image == null) 4 { 5 throw new ArgumentNullException("image"); 6 } 7 if ((image.PixelFormat & PixelFormat.Indexed) != PixelFormat.Undefined) 8 { 9 throw new Exception(SR.GetString("GdiplusCannotCreateGraphicsFromIndexedPixelFormat")); 10 } 11 IntPtr zero = IntPtr.Zero; 12 int status = SafeNativeMethods.Gdip.GdipGetImageGraphicsContext(new HandleRef(image, image.nativeImage), out zero); 13 if (status != 0) 14 { 15 throw SafeNativeMethods.Gdip.StatusException(status); 16 } 17 return new Graphics(zero) { backingImage = image }; 18 }
IntPtr.Zero只是一个常量值,表示空指针。
FromImag()函数返回的是 return new Graphics(zero); 这个相当于null;它的作用只是在bitmap文件上“做画”,写“文字”。