将整个DataGridView保存为图像 [英] Save whole of DataGridView as an Image
本文介绍了将整个DataGridView保存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将整个DataGridView保存为图像.
我在网上看到过一些帖子,但对我不起作用.
到目前为止,我已经尝试了以下两个链接:
I need to save the whole of a DataGridView as an Image.
I have seen some post online but it did not work for me.
So far, I've tried these 2 links:
我的意图是,一旦按下按钮,DataGridView将被转换为图像,并将其自动保存到桌面.
What I intend is that once a Button is pressed, the DataGridView will be converted into an Image and it will be automatically saved to the Desktop.
我使用的代码会产生错误:
The code I'm using generates an error:
GDI +中发生一般错误
A generic error occurred in GDI+
private void button1_Click(object sender, EventArgs e)
{
//Resize DataGridView to full height.
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;
//Create a Bitmap and draw the DataGridView on it.
Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
//Resize DataGridView back to original height.
dataGridView1.Height = height;
//Save the Bitmap to folder.
bitmap.Save(@"C:\\Desktop\\datagrid.jpg");
}
希望获得一些帮助.谢谢!
Hope to get some help. Thanks!
推荐答案
您需要修复代码的多个部分:
You need to fix more than one section of you code:
-
bitmap.Save(@"C:\\ Desktop \\ datagrid.jpg");
.此路径字符串应为:
@"C:\ Users \ SomeUser \ Desktop \ datagrid.jpg"
或
"C:\\ Users \\ SomeUser \\ Desktop\\ datagrid.jpg"
.参见第6点. - 计算DataGridView高度时,不包括网格标题.
- 创建
Bitmap
对象时,必须将该对象与您创建的任何其他一次性对象一样处理.您可以使用使用块. -
Bitmap.Save([Path])
,而不指定 Environment.SpecialFolder桌面.
bitmap.Save(@"C:\\Desktop\\datagrid.jpg");
. This path string should be:
@"C:\Users\SomeUser\Desktop\datagrid.jpg"
or
"C:\\Users\\SomeUser\\Desktop\\datagrid.jpg"
. See point 6.- When calculating the DataGridView height, you're not including the grid Header.
- When creating a
Bitmap
object, that object must be disposed of, as any other disposable object you create. You can use the Bitmap.Dispose() method or enclose your object in a Using block. Bitmap.Save([Path])
, without specifying an ImageFormat, creates aPNG
image. The file extension you insert in the Path string is not considered. At this time, you're creating a file with a.jpg
extension when it's actually a.png
file.- You should use the
Png
format, notJpeg
when saving this kind of bitmap. Its loss-less compression is more adeguate: it will preserve the image colors and improve the overall quality. - The Path to the current user Desktop should not be hard-coded. This path is returned by Environment.SpecialFolder.Desktop.
您可以按以下方式修改代码:
You could modify your code as follows:
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
int DGVOriginalHeight = dataGridView1.Height;
dataGridView1.Height = (dataGridView1.RowCount * dataGridView1.RowTemplate.Height) +
dataGridView1.ColumnHeadersHeight;
using (Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height))
{
dataGridView1.DrawToBitmap(bitmap, new Rectangle(Point.Empty, this.dataGridView1.Size));
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(DesktopFolder, "datagridview1.png"), ImageFormat.Png);
}
dataGridView1.Height = DGVOriginalHeight;
}
这篇关于将整个DataGridView保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文