Last active
June 4, 2021 08:43
-
-
Save kant2002/ed7063050c50fa547e90988d075ddb57 to your computer and use it in GitHub Desktop.
System.Drawing and NativeAOT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SixLabors.ImageSharp; | |
using SixLabors.ImageSharp.PixelFormats; | |
using SixLabors.ImageSharp.Processing; | |
using System; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
namespace NativeAOTImages | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
{ | |
// Magic string which make System.Drawing.Common works. | |
ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance); | |
} | |
// System.Drawing.Common part | |
int width = 128; | |
int height = 128; | |
var file = args[0]; | |
Console.WriteLine($"Loading {file}"); | |
using var pngStream = new FileStream(args[0], FileMode.Open, FileAccess.Read); | |
using var image = new Bitmap(pngStream); | |
var resized = new Bitmap(width, height); | |
using var graphics = Graphics.FromImage(resized); | |
graphics.CompositingQuality = CompositingQuality.HighSpeed; | |
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
graphics.CompositingMode = CompositingMode.SourceCopy; | |
graphics.DrawImage(image, 0, 0, width, height); | |
resized.Save($"resized-{file}", ImageFormat.Png); | |
Console.WriteLine($"Saving resized-{file} thumbnail"); | |
// ImageSharp part. | |
using (SixLabors.ImageSharp.Image<Rgba32> image2 = SixLabors.ImageSharp.Image.Load<Rgba32>(file)) | |
{ | |
image2.Mutate(x => x | |
.Resize(image2.Width / 2, image2.Height / 2) | |
.Grayscale()); | |
image2.Save($"resized-sharp-{file}"); // Automatic encoder selected based on extension. | |
Console.WriteLine($"Saving resized-sharp-{file} thumbnail using ImageSharp"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport("gdiplus.dll", ExactSpelling = true)] | |
internal static extern int GdipCreateBitmapFromStream(IStream stream, out IntPtr bitmap); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: global::System.Runtime.CompilerServices.IgnoresAccessChecksTo("System.Drawing.Common")] | |
namespace System.Runtime.CompilerServices | |
{ | |
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] | |
public class IgnoresAccessChecksToAttribute: Attribute | |
{ | |
public IgnoresAccessChecksToAttribute(string assemblyName) | |
{ | |
this.AssemblyName = assemblyName; | |
} | |
public string AssemblyName { get; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
{ | |
// Magic string which make System.Drawing.Common works. | |
ComWrappers.RegisterForMarshalling(WinFormsComInterop.WinFormsComWrappers.Instance); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TargetFramework>net5.0-windows</TargetFramework> | |
<UseWindowsForms>true</UseWindowsForms> | |
<DisableWinExeOutputInference>true</DisableWinExeOutputInference> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ProjectReference Include="..\System.Drawing.Common\System.Drawing.Common.csproj"> | |
<PrivateAssets>All</PrivateAssets> | |
<Aliases>drawing</Aliases> | |
</ProjectReference> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment