首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将windows窗体的一部分导出为图像

将windows窗体的一部分导出为图像
EN

Stack Overflow用户
提问于 2011-03-10 03:33:13
回答 4查看 12K关注 0票数 1

我有一个表格,其中显示了一个图表,这是在微软图表控件6.0…

我已经在菜单栏中放置了一个选项,可以将生成的图形导出到图像文件中。

谁能告诉我如何将表格的图形部分导出为图像(任何格式都可以)……

我正在考虑截图并保存它,但是我不能让vb中的控件在窗体上的指定区域截图。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-10 04:14:44

下面是它的C#函数

代码语言:javascript
运行
复制
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);
}

并调用

代码语言:javascript
运行
复制
capture(chart1, @"c:\temp.png");

下面是上面转换成VB的c#方法

代码语言:javascript
运行
复制
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
票数 6
EN

Stack Overflow用户

发布于 2013-02-28 10:38:23

尝试以下代码:

Imports System.Drawing.Imaging

代码语言:javascript
运行
复制
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 
票数 1
EN

Stack Overflow用户

发布于 2011-03-10 04:16:37

要查看的关键方法是Control.DrawToBitmap

下面是一个函数,它返回由Bitmap参数指定的控件的函数:

代码语言:javascript
运行
复制
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

您可以像这样使用此函数:

代码语言:javascript
运行
复制
Dim controlImage As Bitmap = GetControlScreenshot(Me.dataGridView)
controlImage.Save("TestImage.bmp") 

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5251155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档