Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy a chart image to the clipboard using C#2010?

Tags:

c#

.net

charts

I am learning C# as I write a program which interfaces with a spectrometer. I have figured out how to get a spectrum from the spectrometer and plot it on an MS chart.

How do I copy the image of the chart into the clipboard so that it can be pasted into other programs?

I'm using Visual Studio C# 2010.

I have found the chart.SaveImage method, but I would really rather copy the image to the clipboard rather than having to save it to disk. I have not found a chart.CopyPicture method.

I also figured out how to copy the raw data to clipboard as a string, which can then be pasted into an Excel worksheet and plotted, but I would rather just copy the image itself.


Additonal data:

I am able to copy the image to the clipboard using the following code:

spectrumChart2.SaveImage("Image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Image img = Image.FromFile("Image.jpg");
System.Windows.Forms.Clipboard.SetImage(img);

Surely there is way to get the image directly Clipboard without saving and retrieving it from a disk file first. Please, please let me know how this is done (before one of my coworkers finds this kludge)!

like image 831
Curt Avatar asked Jan 04 '10 23:01

Curt


3 Answers

To get image from chart control, save it to memory stream, create bitmap and then send it to clipboard:

using (MemoryStream ms = new MemoryStream())
{
    chart1.SaveImage(ms, ChartImageFormat.Bmp);
    Bitmap bm = new Bitmap(ms);
    Clipboard.SetImage(bm);
}
like image 192
pascon Avatar answered Nov 20 '22 14:11

pascon


Use the static method...

Clipboard.SetImage(....);
like image 20
Tim Jarvis Avatar answered Nov 20 '22 12:11

Tim Jarvis


I am saving the chart images as a metafile (EMF) through a memory stream. I want to preserve the original image resolution and preserve the option to ungroup the vector images and edit them in PowerPoint. Recently, I upgraded to a very high-resolution laptop and I found that the chart image was being framed with a lot of empty space. If I paste the image into PowerPoint or Word, the pasted image will be very small. Cropping and resizing manually is not satisfactory, and I don't want to have to downgrade my screen resolution to get the images to paste larger. Here is the solution I found (below).

I am using VB.Net 2008, so the Chart.Clone method was not available. I had to implement a separate function to clone the chart, but if you have a more recent version of Visual Studio, you can uncomment the line below and delete the CloneMSChart function and the line referencing it. Here are references to the relevant websites I used to find the solution:

How to clone a chart

Exporting a high-resolution image

and here is the composite solution:

'-------------------------------------------------------------

Public Sub CopyChartImageToClipBoard(ByVal ChartToSave As Chart)
    Dim originalSize As Size = ChartToSave.Size
    Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim screenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim cloneChart As New Chart

    Using ms As New IO.MemoryStream()
        Try
            'cloneChart = CType(ChartToSave.Clone, Chart)
            cloneChart = CloneMSChart(ChartToSave)
            cloneChart.Size = New Size(screenWidth, screenHeight)   ' copy a high resolution image 
            cloneChart.SaveImage(ms, System.Drawing.Imaging.ImageFormat.Png)
            ms.Seek(0, SeekOrigin.Begin)
            Using mf As New Bitmap(ms)
                Clipboard.SetImage(mf)
            End Using

        Finally
            ms.Close()
            cloneChart.Dispose()
        End Try
    End Using
End Sub

'------------------------------------------------------------

Public Function CloneMSChart(ByVal chart1 As Chart) As Chart
    Dim myStream As New System.IO.MemoryStream
    Dim chart2 As Chart = New Chart
    chart1.Serializer.Save(myStream)
    chart2.Serializer.Load(myStream)
    Return chart2
End Function

'-------------------------------------------------------

like image 37
Robert Cody Avatar answered Nov 20 '22 13:11

Robert Cody