Skip to content

Commit a9123bf

Browse files
authored
[Workspaces] Add encoder parameter to bitmap.save() (#36228)
* [Workspaces] Add encoder parameter to bitmap.save() * 1 more call fixed * Move repeated code to the csharp library
1 parent f7c9c80 commit a9123bf

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Drawing;
6+
using System.Drawing.Imaging;
7+
using System.IO;
8+
using System.Linq;
9+
10+
namespace WorkspacesCsharpLibrary
11+
{
12+
public class DrawHelper
13+
{
14+
public static void SaveBitmap(Bitmap bitmap, MemoryStream memory)
15+
{
16+
ImageCodecInfo imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(codec => codec.FormatID == ImageFormat.Png.Guid);
17+
EncoderParameters encoderParameters = new(1);
18+
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 50);
19+
20+
bitmap.Save(memory, imageCodecInfo, encoderParameters);
21+
}
22+
}
23+
}

src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public BitmapImage IconBitmapImage
132132

133133
using (var memory = new MemoryStream())
134134
{
135-
previewBitmap.Save(memory, ImageFormat.Png);
135+
DrawHelper.SaveBitmap(previewBitmap, memory);
136136
memory.Position = 0;
137137

138138
BitmapImage bitmapImage = new BitmapImage();

src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ Rectangle GetAppRect(Application app)
153153
}
154154

155155
using MemoryStream memory = new();
156-
previewBitmap.Save(memory, ImageFormat.Png);
156+
WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory);
157+
157158
memory.Position = 0;
158159

159160
BitmapImage bitmapImage = new();
@@ -311,7 +312,9 @@ public static BitmapImage DrawPreviewIcons(Project project)
311312
}
312313

313314
using MemoryStream memory = new();
314-
previewBitmap.Save(memory, ImageFormat.Png);
315+
316+
WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory);
317+
315318
memory.Position = 0;
316319

317320
BitmapImage bitmapImage = new();

src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesIcon.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void SaveIcon(Bitmap icon, string path)
9494
FileStream fileStream = new FileStream(path, FileMode.CreateNew);
9595
using (var memoryStream = new MemoryStream())
9696
{
97-
icon.Save(memoryStream, ImageFormat.Png);
97+
WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(icon, memoryStream);
9898

9999
BinaryWriter iconWriter = new BinaryWriter(fileStream);
100100
if (fileStream != null && iconWriter != null)

0 commit comments

Comments
 (0)