kirsan31/winforms-datavisualization

Exported chart looks differently than on-screen

Closed this issue · 7 comments

I have recently switched to .Net 7 and thus I needed to adjust my charting app from .Net Framework to the new platform. Here came your fantastic project for having the good old charting in .Net 7. So far most things are working fine, but today I came upon a strange behaviour. When I export a chart either to hard drive or to clipboard some font sizes are different - in my case smaller and hard to read. Attached you can find examples.

Below is how it looks on-screen:
onscreen

And here you can see how it looks exported:
exported

As seen readability of exported chart is quite bad.

I am using Visual Basic for a Winforms charting application. AutoScaleMode is "DPI". I am developing my app on a laptop with a single Full HD screen (no external screens). When my laptop's font size is 100% there are no issues. When it is 125% the issue is depicted above. In case of 150% the issue is even worse.

To save the chart I use following code:

Private Sub SaveChartAsImage_Click(sender As Object, e As EventArgs) Handles SaveChartAsImage.Click
    Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
    Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)

    Dim parentObject As Windows.Forms.DataVisualization.Charting.Chart = Nothing
    Try
        parentObject = CType(Me.Controls.Find(cms.SourceControl.Name, True)(0), Windows.Forms.DataVisualization.Charting.Chart)
    Catch ex As Exception

    End Try

    SupportingSubs.SaveChartAsImage(parentObject)
End Sub

Public Shared Sub SaveChartAsImage(ch As Windows.Forms.DataVisualization.Charting.Chart)
    Dim SV As SaveFileDialog = New SaveFileDialog()
    Try
        Dim chName As String = ""
        chName = "Chart_" + ch.Titles(0).Text.Replace(" | ", "_")
        chName = chName.Replace("/", "_")
        SV.FileName = chName + ".png"
    Catch
    End Try

    SV.Filter = "PNG Image|*.png|JPeg Image|*.jpg"
    SV.FilterIndex = 1
    SV.RestoreDirectory = True

    Dim result As DialogResult = SV.ShowDialog()

    If result = DialogResult.OK Then

        If SV.FilterIndex = 1 Then
            ch.SaveImage(SV.FileName, Imaging.ImageFormat.Png)
        ElseIf SV.FilterIndex = 2 Then
            ch.SaveImage(SV.FileName, Imaging.ImageFormat.Jpeg)
        End If
    Else
        MsgBox("File path does not exist.")
    End If
End Sub

I am not sure if the problem is related to .Net 7 itself or to the Chart control, but in .Net Framework with the in-built Chart control there were no issues like that.

Do you think if there is a workaround?

Thank you @idenchev.
SaveImage code was not changed since .net framework, so I think it's something related to internal .Net changes. I will look into it when have some time...

I am terribly sorry for opening this issue. It seems that it is related to all .Net (both the new one 7 and the old Framework 4.7). .Net 7 is DpiAware by default. Today I strated digging in my old code (for .Net Framework 4.7) and found that my app was not DpiAware. I made it aware and started experiencing exactly the same issue with smaller fonts after export to file / copying to clipboard / export to Excel. As a result I fix one issue (I used to have a bit blurry graphics when in higher DPIs) and destroyed the export performance. My tests today showed that when exporting with monitor font sizes of 125% and 150% the result file is with the same chart's font sizes like as I did the export with my monitor font size of 100%. I still can't find out how to solve this inconsistency. But this is my problem and not related at all to your great chart control :) You can delete my issue so that it doesn't look like spam.

If it work the same in .net framework it doesn't mean that it's correct behaviour. Can you do an export with 150 dpi, then open this file and zoom it to app size. Then screen app and this file side by side? Will they be look similar?

Below is a comparison in 3 DPI modes how charts look when on-screen (left charts) and when exported (right charts). Very messy :) Please note that for different DPI settings I am controlling the size of the charts and fonts, so they are different.

For exporting I am using following code (the complete one is in my original post):

ch.SaveImage(SV.FileName, Imaging.ImageFormat.Png)

Monitor font size: 100%
_100compar

Monitor font size: 125%
_125compar

Monitor font size: 150%
_150compar

In the meantime thanks to following thread I found a workaround, which gives the result I expect (still testing it though) - https://stackoverflow.com/questions/9641436/saving-higher-resolution-charts-without-messing-up-the-appearance

Dim stream As New System.IO.MemoryStream()
ch.SaveImage(stream, Imaging.ImageFormat.Png)
Dim bmp As New Bitmap(stream)
ch.DrawToBitmap(bmp, New Rectangle(0, 0, 2100, 1500))
bmp.Save(SV.FileName)

Below is a comparison in 3 DPI modes how charts look when on-screen (left charts) and when exported (right charts). Very messy :)

I am afraid that lefts and rights images are the same (same links)....

@idenchev I found the problem - image was always saved with 96 dpi. I will fix this and release with 1.9...