public static void SaveImageToFile(string filepath, Image image, MagickFormat format = MagickFormat.Png) { ImageUInt8RGB byteRgbImage = (ImageUInt8RGB)image.ToRGB().ToUInt8(); uint width = byteRgbImage.Width; uint height = byteRgbImage.Height; byte[,] r = byteRgbImage.R.ToByteArray(); byte[,] g = byteRgbImage.G.ToByteArray(); byte[,] b = byteRgbImage.B.ToByteArray(); byte[] data = new byte[width * height * 3]; for (uint y = 0; y < height; y++) { for (uint x = 0; x < width; x++) { data[3 * width * y + 3 * x] = r[x, y]; data[3 * width * y + 3 * x + 1] = g[x, y]; data[3 * width * y + 3 * x + 2] = b[x, y]; } } var readSettings = new MagickReadSettings { Width = (int)width, Height = (int)height, Format = MagickFormat.Rgb }; var savedImage = new MagickImage(data, readSettings); savedImage.Format = format; savedImage.Write(filepath); }
public override Image ToUInt8() { ImageUInt8RGB uint8Image = new ImageUInt8RGB(Width, Height); double multiplier = (double)byte.MaxValue + 1; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { uint8Image.R[x, y] = (byte)(R[x, y] * multiplier); uint8Image.G[x, y] = (byte)(G[x, y] * multiplier); uint8Image.B[x, y] = (byte)(B[x, y] * multiplier); } } return(uint8Image); }
public static Image LoadFromFile(string filepath) { var image = new MagickImage(filepath); IPixelCollection <byte> pixelCollection = image.GetPixels(); ImageUInt8RGB result = new ImageUInt8RGB((uint)image.Width, (uint)image.Height); for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { IPixel <byte> pixel = pixelCollection.GetPixel(x, y); result.R[x, y] = pixel.GetChannel(0); result.G[x, y] = pixel.GetChannel(1); result.B[x, y] = pixel.GetChannel(2); } } return(result); }