简体   繁体   中英

How to save Graphics as a bitmap image

I was just wondering how to do something, how to save something that is drawn onto a form with Graphics to a bitmap file, everything I have tried so far hasn't worked, just a bit of a background of what I am doing, I am trying to create a random terrain generator, I have got the entire thing working and I just need to know how to save the Graphics as a bitmap, because after that the files will be loaded into another program and used to create a full terrain, at least that's the idea...

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Random_Terrian_1_x_1_pixel
{
public partial class Form1 : Form
{
    int start = 0;
    int count = 0;
    int layer = 251;

    public Form1()
    {
        InitializeComponent();
    }

    private async void button1_Click(object sender, EventArgs e)
    {
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Height: " + this.Height + ", Width: " + this.Width);
    }

    private void placeBlocks1ToolStripMenuItem_Click(object sender, EventArgs e)
    {

        start = start + 10;
        count = count + 1;

        if (count >= 999)
        {
          //  MessageBox.Show("Layer full");
        }
        else
        {
            //this.Text = Convert.ToString(count);
            Random rand = new Random();
            int num = rand.Next(1, 3);
            SolidBrush blockstone = new SolidBrush(Color.Gray);
            SolidBrush blockcoal = new SolidBrush(Color.Black);



            if (num == 1)
            {

                Graphics g = this.CreateGraphics();
                g.FillRectangle(blockstone, new Rectangle(start, layer, 10, 10));
                g.Dispose();


            }
            if (num == 2)
            {

                Graphics g = this.CreateGraphics();
                g.FillRectangle(blockcoal, new Rectangle(start, layer, 10, 10));
                g.Dispose();


            }


        }
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        start = start + 10;
        count = count + 1;

        if (count >= 999)
        {
        //    MessageBox.Show("Layer full");
        }
        else
        {
            //this.Text = Convert.ToString(count);
            Random rand = new Random();
            int num = rand.Next(1, 3);
            SolidBrush blockstone = new SolidBrush(Color.Gray);
            SolidBrush blockcoal = new SolidBrush(Color.Black);



            if (num == 1)
            {

                Graphics g = this.CreateGraphics();
                g.FillRectangle(blockstone, new Rectangle(start, layer, 10, 10));
                g.Dispose();


            }
            if (num == 2)
            {

                Graphics g = this.CreateGraphics();
                g.FillRectangle(blockcoal, new Rectangle(start, layer, 10, 10));
                g.Dispose();


            }
        }

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        if (Convert.ToInt16(textBox1.Text) == 1)
        {
            layer = 251;
            start = 0;
        }
        if (Convert.ToInt16(textBox1.Text) == 2)
        {
            layer = 241;
            start = 0;
        }
        if (Convert.ToInt16(textBox1.Text) == 3)
        {
            layer = 231;
            start = 0;
        }
        if (Convert.ToInt16(textBox1.Text) == 4)
        {
            layer = 221;
            start = 0;
        }
        if (Convert.ToInt16(textBox1.Text) == 5)
        {
            layer = 211;
            start = 0;
        }

    }

    private async void placeByNumberToolStripMenuItem_Click(object sender, EventArgs e)
    {
        int placecount = 0;
        while (placecount <= 19)
        {
            await Task.Delay(250);
            button1.PerformClick();
        }

    }

    private void clearToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Graphics g = this.CreateGraphics();
        g.Clear(SystemColors.Control);
    }

    private void newToolStripButton_Click(object sender, EventArgs e)
    {
        Graphics g = this.CreateGraphics();
        g.Clear(SystemColors.Control);
    }
   }

}

You cannot save a Graphics directly, but you can first create a Bitmap , derive the fresh Graphics from it, draw on the Graphics , and then save the Bitmap instead, like so:

Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);

// Now you have a Graphics you can draw on as much as you want
g.DrawRectangle(...);

// Then, save it
bitmap.Save(@"C:\MyImage.png", ImageFormat.Png);

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