English 中文(简体)
将图形内容保存到文件
原标题:Saving a Graphics content to a file

我有一些代码, 用于将函数图绘制为 < code> PictureBox 图形。 该代码在 < code> paint 事件中执行。 在某些时候, 我想将图形内容的位图保存在文件中 。

我已经读了"的答案, https://stackoverflow.com/ questions/43051168/ savec-graphics-to-bittmap- outside of-paint", 这个问题 , 却找不到我要找的东西。

我所需要的是同时在 PictureBox (或您建议的任何其他控件) 中画出, 这样在控件隐藏时, 我就不会松绑( 所以我认为我不能 < code> Create Graics () < (/code > ) < em> 和 能够将绘图保存在单击按钮的事件中 。

我愿意将绘图逻辑从 Paint 事件中删除,如果没有这种逻辑的话。

提前感谢。

最佳回答

我接着根据我的假设回答了问题:

我创建了一个新的 winforms 应用程序

I added a panel and a button I created a Bitmap named buffer, and in the Form1 constructor, initialized it to the size of the panel. Instead of drawing directly to the panel, I draw to the Bitmap, then set the panels background image buffer; This will add persistance to your graphics. If you truly wish to write to a file, I can show you that. Just ask.

要写入文件, 您需要此命名空间引用 :

using System.IO;

我添加了您要求的 < code> Imaage ToDisc 函数 。

这是代码:

Bitmap buffer;
public Form1()
{
    InitializeComponent();
    panel1.BorderStyle = BorderStyle.FixedSingle;
    buffer = new Bitmap(panel1.Width,panel1.Height);
}

private void button1_Click(object sender, EventArgs e)
{
    using (Graphics g = Graphics.FromImage(buffer))
    {
        g.DrawRectangle(Pens.Red, 100, 100,100,100);
    }

    panel1.BackgroundImage = buffer;
    //writes the buffer Bitmap to a binary file, only neccessary if you want to save to disc
    ImageToDisc();
    //just to prove that it did write it to file and can be loaded I set the mainforms background image to the file
    this.BackgroundImage=FileToImage();
}

//Converts the image to a byte[] and writes it to disc
public void ImageToDisc()
{
    ImageConverter converter = new ImageConverter();
    File.WriteAllBytes(@"c:	est.dat", (byte[])converter.ConvertTo(buffer, typeof(byte[])));
}

//Converts the image from disc to an image
public Bitmap FileToImage()
{
    ImageConverter converter = new ImageConverter();
    return (Bitmap)converter.ConvertFrom(File.ReadAllBytes(@"c:	est.dat"));
}
问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签