直接用GDI绘图保存时picturebox.image为空,下面是解决方法!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace 算法验证图像生成
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Width = 512;
pictureBox1.Height = 256;
pictureBox1.Top = 32;
pictureBox1.Left =32;
pictureBox2.Top = pictureBox1.Height + pictureBox1.Top;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
Graphics g = Graphics.FromImage((Image)bmp);
g.FillRectangle(new SolidBrush(Color.Gainsboro), 0, 0, 512,256);
pictureBox1.Image = (Image)bmp;
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
//pictureBox1.Image.Save("111.bmp");
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "保存图片";
dlg.OverwritePrompt = true;
dlg.InitialDirectory = "c:\\pic\\";
dlg.Filter = "*.bmp|*.bmp";
dlg.FilterIndex = 2;
if (dlg.ShowDialog() == DialogResult.OK)
{
string temp = dlg.FileName ;
pictureBox1.Image.Save(temp);
}
}
}
}
感谢zxcayumi() 无私的帮助,十分感激!
希望看过的朋友能给出更好的解决方案!