diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/DrawHelper.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/DrawHelper.cs new file mode 100644 index 0000000000..30fa3493dd --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/DrawHelper.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; + +namespace WorkspacesCsharpLibrary +{ + public class DrawHelper + { + public static void SaveBitmap(Bitmap bitmap, MemoryStream memory) + { + ImageCodecInfo imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(codec => codec.FormatID == ImageFormat.Png.Guid); + EncoderParameters encoderParameters = new(1); + encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 50); + + bitmap.Save(memory, imageCodecInfo, encoderParameters); + } + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs index 81437fb845..45bc2ef89e 100644 --- a/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs @@ -132,7 +132,7 @@ namespace WorkspacesCsharpLibrary.Models using (var memory = new MemoryStream()) { - previewBitmap.Save(memory, ImageFormat.Png); + DrawHelper.SaveBitmap(previewBitmap, memory); memory.Position = 0; BitmapImage bitmapImage = new BitmapImage(); diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs index e70324567d..964ee0bc4f 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs @@ -153,7 +153,8 @@ namespace WorkspacesEditor.Utils } using MemoryStream memory = new(); - previewBitmap.Save(memory, ImageFormat.Png); + WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory); + memory.Position = 0; BitmapImage bitmapImage = new(); @@ -311,7 +312,9 @@ namespace WorkspacesEditor.Utils } using MemoryStream memory = new(); - previewBitmap.Save(memory, ImageFormat.Png); + + WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory); + memory.Position = 0; BitmapImage bitmapImage = new(); diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesIcon.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesIcon.cs index 1e85aec9cc..9750d23b83 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesIcon.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesIcon.cs @@ -94,7 +94,7 @@ namespace WorkspacesEditor.Utils FileStream fileStream = new FileStream(path, FileMode.CreateNew); using (var memoryStream = new MemoryStream()) { - icon.Save(memoryStream, ImageFormat.Png); + WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(icon, memoryStream); BinaryWriter iconWriter = new BinaryWriter(fileStream); if (fileStream != null && iconWriter != null)