locked
Saving a chart as an image using a savefiledialog - VB.NET RRS feed

  • Question

  • How do I save a chart control as an image file using a savefiledialog? This is how I'd like to code to be set out.

    User clicks button > SaveFileDialog shows > User can select PNG, JPEG, GIF, etc. > User saves chart as image in file location

    Thanks in advance!


    JDS404 Check out my blog at www.interlinkjds.wordpress.com!

    • Moved by Amanda Zhu Wednesday, June 25, 2014 9:41 AM better forum
    Tuesday, June 24, 2014 5:35 PM

All replies

  • Wednesday, June 25, 2014 5:27 AM
  • Because your issue is about chart control, I moved this thread to Chart Controls for .NET Framework forum for better support.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 25, 2014 9:41 AM
  • See Save_Click: http://code.msdn.microsoft.com/Windows-Forms-Samples-26bf2a53/sourcecode?fileId=114478&pathId=2020417988

    You can also download and investigate the sample.

    I've used the code you supplied:

     ' Create a new save file dialog
       Dim saveFileDialog1 As New SaveFileDialog()
       
       ' Sets the current file name filter string, which determines 
       ' the choices that appear in the "Save as file type" or 
       ' "Files of type" box in the dialog box.
       saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|SVG (*.svg)|*.svg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif"
       saveFileDialog1.FilterIndex = 2
       saveFileDialog1.RestoreDirectory = True
       
       ' Set image file format
       If saveFileDialog1.ShowDialog() = DialogResult.OK Then
          Dim format As ChartImageFormat = ChartImageFormat.Bmp
          
          If saveFileDialog1.FileName.EndsWith("bmp") Then
             format = ChartImageFormat.Bmp
          Else
             If saveFileDialog1.FileName.EndsWith("jpg") Then
                format = ChartImageFormat.Jpeg
             Else
                If saveFileDialog1.FileName.EndsWith("emf") Then
                   format = ChartImageFormat.Emf
                Else
                   If saveFileDialog1.FileName.EndsWith("gif") Then
                      format = ChartImageFormat.Gif
                   Else
                      If saveFileDialog1.FileName.EndsWith("png") Then
                         format = ChartImageFormat.Png
                      Else
                         If saveFileDialog1.FileName.EndsWith("tif") Then
                            format = ChartImageFormat.Tiff
                         Else
                            If saveFileDialog1.FileName.EndsWith("svg") Then
                               format = ChartImageFormat.Svg
                            End If
                         End If 
                      End If ' Save image
                   End If
                End If
             End If
          End If
          Chart1.SaveImage(saveFileDialog1.FileName, format)
       End If
    

    But the image saves as a white box instead of the chart being displayed. Can you help?

    Thanks again.


    JDS404 Check out my blog at www.interlinkjds.wordpress.com!

    Wednesday, June 25, 2014 3:37 PM
  • Maybe this Chart1 variable is not a chart that is displayed on the form and contains data. The image is a white box when the chart does not contain any information.

    Wednesday, June 25, 2014 5:59 PM
  • Maybe this Chart1 variable is not a chart that is displayed on the form and contains data. The image is a white box when the chart does not contain any information.

    This is what the chart looks like:

    By the way, the chartcontrol isn't placed in the IDE, it is created when the form loads, could that have something to do with it?

    Thanks.


    JDS404 Check out my blog at www.interlinkjds.wordpress.com!

    Wednesday, June 25, 2014 6:14 PM