将部分 Windows 窗体导出为图像

发布于 2024-10-21 06:04:26 字数 188 浏览 2 评论 0原文

我有一个表格,显示在 Microsoft Chart 控件 6.0 中制作的图表...

我在菜单栏中放置了一个选项,它将导出制作的图表到图像文件...

有人能告诉我如何导出图表部分吗将表单作为图像(任何格式都可以)...

我正在考虑截取屏幕截图并保存它,但我无法在 VB 中获取控件来截取表单上指定区域的屏幕截图。

I have a form which shows a graph which was made in Microsoft Chart control 6.0...

I have placed a option in the menubar which will export the graph made to an image file...

Can some one tell how to export the graph part of the form as a image (any format will do)...

I was thinking of taking a screenshot and saving it but i cudnt get the controls in vb to take a screenshot of a specified area on the form.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫弦 2024-10-28 06:04:26

这是它的 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

here's the C# function for it

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);
}

and call

capture(chart1, @"c:\temp.png");

Here's the above c# method converted to VB

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
ま柒月 2024-10-28 06:04:26

尝试以下代码:

导入 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 

Try this code:

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 
不必在意 2024-10-28 06:04:26

要查看的关键方法是 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") 

代码有点粗糙,但我相信它为您指明了正确的方向。

The key method to look at is Control.DrawToBitmap.

Here's a function that returns an Bitmap of a control specified by the function parameter:

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

You can use this function like this:

Dim controlImage As Bitmap = GetControlScreenshot(Me.dataGridView)
controlImage.Save("TestImage.bmp") 

The code's a bit rough but I believe it points you in the right direction.

丶视觉 2024-10-28 06:04:26

阅读 Visual Basic 文档,如何找到类之间的链接控件并不明显。
我假设实现使用表单保存 PaintEvent 的主要命令是

  1. 表单控件的名称,如下所示,在
  2. 连接到 size 属性命令的
  3. 第五行中,将 .CreateGraphics() 命令命名为表单 我。 属性

。我的下一个假设是 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://learn.microsoft.com/de-at/dotnet/desktop/winforms/how-to-create-a-windows-forms-application-from-the-command-line?view=netframeworkdesktop-4.8

It is not obvious, reading visual basic documentations, how to find the linked control between the classes.
I assume the main commands to achieve saving a paintevent with a form are

  1. the name for the form control, below, in line five
  2. connected to the size property command names
  3. the .CreateGraphics() command as form Me. property

. My next assumption is the creategraphics add an image to
a part of a control logic. To change the appearance of a
,for example, button1 control creategraphics is to consider
the development environment.

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

The above code is tested. The paintevent can be saved. For compiling i used Create a Windows Forms application from the command line,

https://learn.microsoft.com/de-at/dotnet/desktop/winforms/how-to-create-a-windows-forms-application-from-the-command-line?view=netframeworkdesktop-4.8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文