我有一个表格,其中显示了一个图表,这是在微软图表控件6.0…
我已经在菜单栏中放置了一个选项,可以将生成的图形导出到图像文件中。
谁能告诉我如何将表格的图形部分导出为图像(任何格式都可以)……
我正在考虑截图并保存它,但是我不能让vb中的控件在窗体上的指定区域截图。
发布于 2011-03-10 04:14:44
下面是它的C#函数
private void capture(Control ctrl, string fileName)
{
Rectangle bounds = ctrl.Bounds;
Point pt = ctrl.PointToScreen(bounds.Location);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size);
}
bitmap.Save(fileName,ImageFormat.Png);
}
并调用
capture(chart1, @"c:\temp.png");
下面是上面转换成VB的c#方法
Private Sub capture(ctrl As Control, fileName As String)
Dim bounds As Rectangle = ctrl.Bounds
Dim pt As Point = ctrl.PointToScreen(bounds.Location)
Dim bitmap As New Bitmap(bounds.Width, bounds.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(New Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size)
End Using
bitmap.Save(fileName, ImageFormat.Png)
End Sub
发布于 2013-02-28 10:38:23
尝试以下代码:
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
' Take a screenshot of the entire Form1
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot
bmpScreenshot.Save("D:\Form1.jpg", ImageFormat.Jpeg)
End Sub
End Class
发布于 2011-03-10 04:16:37
要查看的关键方法是Control.DrawToBitmap。
下面是一个函数,它返回由Bitmap
参数指定的控件的函数:
Private Function GetControlScreenshot(ByVal control As Control) As Bitmap
Dim g As Graphics = control.CreateGraphics()
Dim bitmap As Bitmap = New Bitmap(control.Width, control.Height)
control.DrawToBitmap(bitmap, New Rectangle(control.Location, control.Size))
GetControlScreenshot = bitmap
End Function
您可以像这样使用此函数:
Dim controlImage As Bitmap = GetControlScreenshot(Me.dataGridView)
controlImage.Save("TestImage.bmp")
代码有点粗糙,但我相信它为您指明了正确的方向。
https://stackoverflow.com/questions/5251155
复制相似问题