public static BitmapSource GetBitmap(this IImageSource imageSource) { if (imageSource.HasImage == false) { return(null); } Byte[] data = imageSource.MergeChannels(); var channelCount = imageSource.Channels.Length; var pitch = imageSource.Width * imageSource.Channels.Length; var w = imageSource.Width; var h = imageSource.Height; //var format = channelCount == 3 ? TextureFormat.RGB24 : TextureFormat.ARGB32; //var tex = new Texture2D(w, h, format, false); var colors = new Color[data.Length / channelCount]; var k = 0; for (var y = h - 1; y >= 0; --y) { for (var x = 0; x < pitch; x += channelCount) { var n = x + y * pitch; var c = Color.FromArgb(1, 1, 1, 1); if (channelCount == 4) { c.B = data[n++]; c.G = data[n++]; c.R = data[n++]; c.A = (Byte)System.Math.Round(data[n++] / 255f * imageSource.Opacity * 255f); } else { c.B = data[n++]; c.G = data[n++]; c.R = data[n++]; c.A = (Byte)System.Math.Round(imageSource.Opacity * 255f); } colors[k++] = c; } } if (channelCount == 4) { return(BitmapSource.Create(imageSource.Width, imageSource.Height, 96, 96, PixelFormats.Bgra32, null, data, pitch)); } return(BitmapSource.Create(imageSource.Width, imageSource.Height, 96, 96, PixelFormats.Bgr24, null, data, pitch)); }
public async Task <Image <Rgba32> > LoadImageAsync(string fileName) { byte[] source = await File.ReadAllBytesAsync(fileName); await using (MemoryStream ms = new(buffer : source, writable : false)) { using (PsdDocument psd = PsdDocument.Create(ms)) { IImageSource imageSource = psd; byte[] data = imageSource.MergeChannels(); if (imageSource.Channels.Length == 4) { return(Convert(Image.LoadPixelData <Bgra32>(data: data, width: imageSource.Width, height: imageSource.Height))); } return(Convert(Image.LoadPixelData <Bgr24>(data: data, width: imageSource.Width, height: imageSource.Height))); } } }
public static Bitmap GetBitmap(this IImageSource imageSource) { if (imageSource.HasImage == false) { return(null); } byte[] data = imageSource.MergeChannels(); var channelCount = imageSource.Channels.Length; var pitch = imageSource.Width * channelCount; var w = imageSource.Width; var h = imageSource.Height; //var format = channelCount == 3 ? TextureFormat.RGB24 : TextureFormat.ARGB32; bool is4chan = channelCount == 4; var bitmap = new Bitmap(w, h); for (var y = h - 1; y >= 0; --y) { for (var x = 0; x < pitch; x += channelCount) { var n = x + y * pitch; var b = data[n++]; var g = data[n++]; var r = data[n++]; var a = is4chan ? (byte)System.Math.Round(data[n++] / 255f * imageSource.Opacity * 255f) : (byte)System.Math.Round(imageSource.Opacity * 255f); var color = Color.FromArgb(a, r, g, b); bitmap.SetPixel(x / channelCount, y, color); } } return(bitmap); }