2010-05-23 91 views

回答

12

你需要的图片,以吸引你的图形,所以你可能已经拥有了像:

Graphics g = Graphics.FromImage(image); 
+0

谢谢你的回答 – 2010-05-23 15:42:26

+0

谢谢您的回答 – 2010-05-23 15:42:43

+1

这是问题的相反的情况:问题是:图形到图像,不形象的图形,你回答 – 2017-08-24 06:33:31

1

如果你在一个控制的图形直接绘制,你可以创建一个新的位图与控件相同的尺寸,然后调用Control.DrawToBitmap()。然而,更好的方法通常是从一个位图开始,绘制到其图形上(如Darin所建议的),然后将位图绘制到控件上。

24

由于Graphics对象不包含任何图像数据,因此无法将Graphics对象转换为图像。

Graphics对象只是一个用于在画布上绘制的工具。该画布通常是Bitmap对象或屏幕。

如果Graphics对象用于在Bitmap上绘图,那么您已经拥有该图像。如果使用Graphics对象在屏幕上绘图,则必须进行屏幕截图以获取画布图像。

如果Graphics对象是从一个窗口控件创建,您可以使用控件的方法DrawToBitmap渲染图像上,而不是屏幕上的控制。

+0

谢谢您的回答 – 2010-05-23 15:41:26

+0

和你解释 – 2010-05-23 15:43:32

+0

@Sorush:我修好了它。 (为了将来的参考,如果你打算评论,以便Hesam会得到通知,你应该对问题发表评论,而不是回答。) – Guffa 2010-05-25 12:57:49

12

由于达林说,你可能已经有了这个形象。如果不这样做,你可以创建一个新的,并绘制到一个

Image bmp = new Bitmap(width, height); 
using (Graphics g = Graphics.FromImage(bmp)) { 
    // draw in bmp using g 
} 
bmp.Save(filename); 

Save图像保存到硬盘驱动器上的文件。

+0

感谢您的回答 – 2010-05-23 15:44:46

0

把图形转换成位图的最好方法是摆脱了“使用”的东西:

 Bitmap b1 = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height); 
     Graphics g = Graphics.FromImage(b1); 
     g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); 
     b1.Save("screen.bmp"); 

我发现这一点的同时搞清楚如何将图形转换成位图,它就像一个魅力。

我对如何使用这一些例子:

//1. Take a screenshot 
    Bitmap b1 = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height); 
    Graphics g = Graphics.FromImage(b1); 
    g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); 
    b1.Save("screen.bmp"); 

    //2. Create pixels (stars) at a custom resolution, changing constantly like stars 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     /* 
     * Steps to use this code: 
     * 1. Create new form 
     * 2. Set form properties to match the settings below: 
     *  AutoSize = true 
     *  AutoSizeMode = GrowAndShrink 
     *  MaximizeBox = false 
     *  MinimizeBox = false 
     *  ShowIcon = false; 
     *  
     * 3. Create picture box with these properties: 
     *  Dock = Fill 
     * 
     */ 

     //<Definitions> 
     Size imageSize = new Size(400, 400); 
     int minimumStars = 600; 
     int maximumStars = 800; 
     //</Definitions> 

     Random r = new Random(); 
     Bitmap b1 = new Bitmap(imageSize.Width, imageSize.Height); 
     Graphics g = Graphics.FromImage(b1); 
     g.Clear(Color.Black); 
     for (int i = 0; i <r.Next(minimumStars, maximumStars); i++) 
     { 
      int x = r.Next(1, imageSize.Width); 
      int y = r.Next(1, imageSize.Height); 
      b1.SetPixel(x, y, Color.WhiteSmoke); 
     } 
     pictureBox1.Image = b1; 

    } 

有了这个代码,您可以使用所有的图形类的命令,并将其复制到一个位图,因此,让您保存任何设计与图形类。

您可能会使用这个优势。