简体   繁体   中英

Convert Image to Graphic in c#

如何将图像转换为图形?

You can't convert a Graphics object into an image, as the Graphics object doesn't contain any image data.

The Graphics object is just a tool used to draw on a canvas. That canvas is typically a Bitmap object or the screen.

If the Graphics object is used for drawing on a Bitmap , then you already have the image. If the Graphics object is used for drawing on the screen, you would have to make a screen shot to get an image of the canvas.

If the Graphics object was created from a window control, you could use the control's DrawToBitmap method to render the control on an image instead of on the screen.

你需要一个Image来绘制你的图形,所以你可能已经有了图像:

Graphics g = Graphics.FromImage(image);

As Darin states, you probably already have the image. If you don't, you can create a new one and draw to that one

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

Save saves the image to a file on your hard drive.

If you're drawing directly on a Control's graphics, you can create a new Bitmap with the same dimensions as the control and then call Control.DrawToBitmap(). However, the better way to go is usually to start with a Bitmap, draw to its graphics (as suggested by Darin), and then paint the bitmap onto the Control.

The best method to turn graphics into a bitmap is to get rid of the 'using' stuff:

        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");

I discovered this while figuring out how to turn graphics into a bitmap, and it works like a charm.

I have some examples on how to use this:

    //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;

    }

With this code, you can use all the commands for the Graphics Class, and copy them to a bitmap, therefore allowing you to save anything designed with the graphics class.

You may use this to your advantage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM