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.
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
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
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.
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
- the name for the form control, below, in line five
- connected to the size property command names
- the
.CreateGraphics()
command as formMe.
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
精彩评论