Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspaces] Add encoder parameter to bitmap.save() #36228

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/modules/Workspaces/WorkspacesCsharpLibrary/DrawHelper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public BitmapImage IconBitmapImage

using (var memory = new MemoryStream())
{
previewBitmap.Save(memory, ImageFormat.Png);
DrawHelper.SaveBitmap(previewBitmap, memory);
memory.Position = 0;

BitmapImage bitmapImage = new BitmapImage();
Expand Down
7 changes: 5 additions & 2 deletions src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ Rectangle GetAppRect(Application app)
}

using MemoryStream memory = new();
previewBitmap.Save(memory, ImageFormat.Png);
WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory);

memory.Position = 0;

BitmapImage bitmapImage = new();
Expand Down Expand Up @@ -311,7 +312,9 @@ public static BitmapImage DrawPreviewIcons(Project project)
}

using MemoryStream memory = new();
previewBitmap.Save(memory, ImageFormat.Png);

WorkspacesCsharpLibrary.DrawHelper.SaveBitmap(previewBitmap, memory);

memory.Position = 0;

BitmapImage bitmapImage = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static void SaveIcon(Bitmap icon, string path)
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)
Expand Down
Loading