vb.netchartssavefiledialogsave-image

Saving image from a chart


I need to program a button to save a graph, generated with my software, as an image. I looked around and I've found Chart.SaveImage command, so I programmed the abovementioned button as follows:

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    SaveFileDialog2.Filter = "PNG Image (*.png*)|*.png|JPEG Image (*.jpg*)|*.jpg|Bitmap Image (*.bmp*)|*.bmp|TIFF Image (*.tiff*)|*.tiff"
    Select Case Path.GetExtension(SaveFileDialog2.FileName)
        Case ".png"
            Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Png)
        Case ".jpg"
            Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Jpeg)
        Case ".bmp"
            Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Bmp)
        Case ".tiff"
            Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Tiff)
    End Select
End Sub

While debugging, when I press the button, it seems that it hasn't been programmed because at the pressure nothing seems to happen. What I need, as you can understand from my code, is that a SaveFile Dialog appears and I can choose where to save the image and its name and format. Thanks for any answers or comments. Best regards.


Solution

  • Use as follows:

    You forgot to call SaveFileDialog2.ShowDialog. So without showing SaveFileDialog window that allows you to chose a path nothing happens.

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        SaveFileDialog2.Filter = "PNG Image (*.png*)|*.png|JPEG Image (*.jpg*)|*.jpg|Bitmap Image (*.bmp*)|*.bmp|TIFF Image (*.tiff*)|*.tiff"
        If SaveFileDialog2.ShowDialog = DialogResult.OK Then
            Select Case Path.GetExtension(SaveFileDialog2.FileName)
                Case ".png"
                    Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Png)
                Case ".jpg"
                    Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Jpeg)
                Case ".bmp"
                    Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Bmp)
                Case ".tiff"
                    Chart1.SaveImage(SaveFileDialog2.FileName, ChartImageFormat.Tiff)
            End Select
        End If
    End Sub