我只是想知道如何做某事,如何将带有图形的表格上的东西保存到位图文件中,到目前为止我尝试的所有内容都没有用,只是有点背景知识我正在做,我正在尝试创建一个随机地形生成器,我已经完成整个工作,我只需要知道如何将图形保存为位图,因为之后文件将被加载到另一个程序并用于创造一个完整的地形,至少是这个想法...
这是我的代码:
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);
}
}
}
答案 0 :(得分:1)
您无法直接保存Graphics
,但可以先创建Bitmap
,从中获取新的Graphics
,在Graphics
上绘制,然后保存Bitmap
代替,如此:
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);