我有一个表单,显示在Microsoft Chart control 6.0中生成的图表...
我在菜单栏中放置了一个选项,它会将图形导出到图像文件...
有人可以告诉我如何将表单的图形部分导出为图像(任何格式都可以)...
我正在考虑拍摄截图并保存它,但我在vb中获取控件以获取表单上指定区域的屏幕截图。
答案 0 :(得分:6)
这是它的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
答案 1 :(得分:1)
试试这段代码:
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
答案 2 :(得分:0)
要查看的关键方法是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")
代码有点粗糙,但我相信它会指出你正确的方向。
答案 3 :(得分:0)
不是很明显,看visual basic文档,如何找到类之间的链接控件。 我假设实现使用表单保存paintevent的主要命令是
.CreateGraphics()
命令作为表单 Me.
属性。我的下一个假设是 creategraphics 添加一个图像 控制逻辑的一部分。改变外观 ,比如button1控件creategraphics就是考虑 开发环境。
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Dim image1 As New Bitmap(Me.Width, Me.Height)
Sub Seitenlogik(e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.Space
image1.Save("C:\Users\folder_name\OneDrive\Pictures\paper.bmp", _
System.Drawing.Imaging.ImageFormat.Bmp)
image1.Dispose()
End Select
End Sub
Public Sub New()
With Me
.Scale(new SizeF(1, 1))
.FormBorderStyle = FormBorderStyle.None
.StartPosition = FormStartPosition.Manual
.Location = New Point(50, 50)
.CreateGraphics()
.DrawToBitmap(image1, New Rectangle(0, 0, Me.Width, Me.Height))
End With
End Sub
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Seitenlogik(e)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
With e.Graphics
.DrawString("What do i have to proceed?", New Font("Consolas", 22),
New SolidBrush(Color.FromArgb(184, 190, 132, 230)), New Point(50, 50))
End With
End Sub
End Class
以上代码经过测试。可以保存paintevent。对于编译,我使用了从命令行创建 Windows 窗体应用程序,
https://docs.microsoft.com/de-at/dotnet/desktop/winforms/how-to-create-a-windows-forms-application-from-the-command-line?view=netframeworkdesktop-4.8