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

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

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)!


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

Use the static method...


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()
            '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)
            End Using

        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
    Return chart2
End Function


Need Your Help

How do I run my Android custom build type?

android build gradle android-studio

So I am trying to get my project to generate a signed and aligned apk and run it on my phone all through the Android Studio IDE.

"Unable to launch the IIS Express Web server" error visual-studio-2012 windows-8 iis-express

I receive this error when trying to launch IIS Express from Visual Studio with a project that's configured to listen to an address other than localhost. Visual Studio freezes for about 30 seconds ...