/// <summary> /// 位图转文本 /// </summary> /// <param name="item"></param> /// <returns></returns> public static List <string> ImageToText(Bitmap item) { List <string> text = new List <string>(); StringBuilder line = new StringBuilder(); using (MagickImage image = new MagickImage(item)) using (IPixelCollection pixels = image.GetPixels()) { //获取RGB像素数组 byte[] areaPixels = pixels.ToByteArray(PixelMapping.RGB); //转为单通道数据数组 byte[] data = GetSingleChannelData(areaPixels, 0); //图像转文本 int index = 0; for (int i = 0; i < image.Height; i++) { index = i * image.Width; for (int j = 0; j < image.Width; j++) { line.Append(data[index + j] == 255 ? "1" : "0");//黑0,白1 } text.Add(line.ToString()); line.Clear(); } } return(text); }
public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : unmanaged, IPixel <TPixel> { using var magickImage = new MagickImage(stream); var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); IMemoryGroup <TPixel> resultPixels = result.GetRootFramePixelBuffer().FastMemoryGroup; using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) { if (magickImage.Depth == 8) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); FromRgba32Bytes(configuration, data, resultPixels); } else if (magickImage.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, resultPixels); } else { throw new InvalidOperationException(); } } return(result); }
public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : struct, IPixel <TPixel> { using (var magickImage = new MagickImage(stream)) { var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); Span <TPixel> resultPixels = result.GetPixelSpan(); using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) { if (magickImage.Depth == 8) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); PixelOperations <TPixel> .Instance.PackFromRgba32Bytes(data, resultPixels, resultPixels.Length); } else if (magickImage.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); PixelOperations <TPixel> .Instance.PackFromRgba64Bytes(bytes, resultPixels, resultPixels.Length); } else { throw new InvalidOperationException(); } } return(result); } }
/// <summary> /// Converts this instance to a <see cref="Bitmap"/> using <see cref="ImageFormat.Bmp"/>. /// </summary> /// <returns>A <see cref="Bitmap"/> that has the format <see cref="ImageFormat.Bmp"/>.</returns> public Bitmap ToBitmap() { if (ColorSpace == ColorSpace.CMYK) { ColorSpace = ColorSpace.sRGB; } string mapping = "BGR"; PixelFormat format = PixelFormat.Format24bppRgb; if (HasAlpha) { mapping = "BGRA"; format = PixelFormat.Format32bppArgb; } using (IPixelCollection pixels = GetPixelsUnsafe()) { Bitmap bitmap = new Bitmap(Width, Height, format); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, format); IntPtr destination = data.Scan0; for (int y = 0; y < Height; y++) { byte[] bytes = pixels.ToByteArray(0, y, Width, 1, mapping); Marshal.Copy(bytes, 0, destination, bytes.Length); destination = new IntPtr(destination.ToInt64() + data.Stride); } bitmap.UnlockBits(data); return(bitmap); } }
/// <summary> /// Converts this instance to a <see cref="BitmapSource"/>. /// </summary> /// <returns>A <see cref="BitmapSource"/>.</returns> public BitmapSource ToBitmapSource() { string mapping = "RGB"; MediaPixelFormat format = MediaPixelFormats.Rgb24; if (HasAlpha) { mapping = "BGRA"; format = MediaPixelFormats.Bgra32; } if (ColorSpace == ColorSpace.CMYK) { mapping = "CMYK"; format = MediaPixelFormats.Cmyk32; } int step = format.BitsPerPixel / 8; int stride = Width * step; using (IPixelCollection pixels = GetPixelsUnsafe()) { byte[] bytes = pixels.ToByteArray(mapping); return(BitmapSource.Create(Width, Height, 96, 96, format, null, bytes, stride)); } }
public void ShouldReturnNullWhenGeometryIsSpecifiedAndMappingIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToByteArray(new MagickGeometry(1, 2, 3, 4), null); Assert.IsNull(values); } } }
public void ShouldThrowExceptionWhenXTooLow() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { if (Is64Bit) { pixels.ToByteArray(-1, 0, 1, 1, "RGB"); } else { ExceptionAssert.Throws <OverflowException>(() => { pixels.ToByteArray(-1, 0, 1, 1, "RGB"); }); } } } }
public void ShouldReturnNullWhenGeometryIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToByteArray(null, "RGB"); Assert.IsNull(values); } } }
public void ShouldReturnArrayWhenTwoChannelsAreSupplied() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToByteArray("RG"); int length = image.Width * image.Height * 2; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws <MagickResourceLimitErrorException>(() => { pixels.ToByteArray(string.Empty); }); } } }
public void ShouldReturnArrayWhenGeometryIsCorrect() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToByteArray(new MagickGeometry(10, 10, 113, 108), "RG"); int length = 113 * 108 * 2; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenGeometryIsSpecifiedAndMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws <MagickResourceLimitErrorException>(() => { var values = pixels.ToByteArray(new MagickGeometry(1, 2, 3, 4), string.Empty); }); } } }
public void ToByteArray_GeometryIsValidMappingIsEmpty_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentException("mapping", () => { pixels.ToByteArray(new MagickGeometry(1, 2, 3, 4), string.Empty); }); } } }
public void ToByteArray_MappingIsEmpty_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.ThrowsArgumentException("mapping", () => { pixels.ToByteArray(string.Empty); }); } } }
public void ToByteArray_AreaIsCorrect_ReturnsArray() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToByteArray(60, 60, 63, 58, "RGBA"); int length = 63 * 58 * 4; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenGeometryIsSpecifiedAndMappingIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <ArgumentNullException>("mapping", () => { pixels.ToByteArray(new MagickGeometry(1, 2, 3, 4), null); }); } } }
public void ShouldReturnPixelsWhenAreaIsCorrectAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToByteArray(60, 60, 63, 58, PixelMapping.RGBA); int length = 63 * 58 * 4; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenGeometryIsNullAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <ArgumentNullException>("geometry", () => { pixels.ToByteArray(null, PixelMapping.RGB); }); } } }
public void ShouldReturnArrayWhenTwoChannelsAreSuppliedAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToByteArray(PixelMapping.RGB); int length = image.Width * image.Height * 3; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenXTooLow() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <ArgumentOutOfRangeException>("x", () => { pixels.ToByteArray(-1, 0, 1, 1, "RGB"); }); } } }
public void ShouldThrowExceptionWhenMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <ArgumentException>("mapping", () => { pixels.ToByteArray(string.Empty); }); } } }
public void ShouldThrowExceptionWhenMappingIsInvalid() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <MagickOptionErrorException>(() => { pixels.ToByteArray("FOO"); }); } } }
public void ShouldThrowExceptionWhenMappingIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentNullException("mapping", () => { pixels.ToByteArray(null); }); } } }
public void ShouldReturnArrayWhenGeometryIsCorrectAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToByteArray(new MagickGeometry(10, 10, 113, 108), PixelMapping.RGB); int length = 113 * 108 * 3; Assert.AreEqual(length, values.Length); } } }
public void ToByteArray_GeometryIsNull_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentNullException("geometry", () => { pixels.ToByteArray(null, "RGB"); }); } } }
public void ToByteArray_TwoChannels_ReturnsArray() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToByteArray("RG"); int length = image.Width * image.Height * 2; Assert.AreEqual(length, values.Length); } } }
public void ToByteArray_InvalidMapping_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws<MagickOptionErrorException>(() => { pixels.ToByteArray("FOO"); }); } } }
static void Main(string[] args) { int componentX = System.Convert.ToInt32(args[0]); int componentY = System.Convert.ToInt32(args[1]); string pathToFile = args[2]; MagickImage im = new MagickImage(pathToFile); IPixelCollection pc = im.GetPixels(); byte[] pixels = pc.ToByteArray(0, 0, im.Width, im.Height, PixelMapping.RGBA); Console.WriteLine(encode(pixels, im.Width, im.Height, componentX, componentY)); }
public Bitmap ToBitmap(BitmapDensity bitmapDensity) { IMagickImage image = this; string mapping = "BGR"; var format = PixelFormat.Format24bppRgb; try { if (image.ColorSpace != ColorSpace.sRGB) { image = Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { mapping = "BGRA"; format = PixelFormat.Format32bppArgb; } using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var bitmap = new Bitmap(image.Width, image.Height, format); var data = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, format); var destination = data.Scan0; for (int y = 0; y < Height; y++) { byte[] bytes = pixels.ToByteArray(0, y, Width, 1, mapping); Marshal.Copy(bytes, 0, destination, bytes.Length); destination = new IntPtr(destination.ToInt64() + data.Stride); } bitmap.UnlockBits(data); SetBitmapDensity(bitmap, bitmapDensity); return(bitmap); } } finally { if (!ReferenceEquals(this, image)) { image.Dispose(); } } }
public BitmapSource ToBitmapSource(BitmapDensity bitmapDensity) { IMagickImage image = this; var mapping = "RGB"; var format = MediaPixelFormats.Rgb24; try { if (ColorSpace == ColorSpace.CMYK && !image.HasAlpha) { mapping = "CMYK"; format = MediaPixelFormats.Cmyk32; } else { if (ColorSpace != ColorSpace.sRGB) { image = Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { mapping = "BGRA"; format = MediaPixelFormats.Bgra32; } } var step = format.BitsPerPixel / 8; var stride = Width * step; using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var bytes = pixels.ToByteArray(mapping); var dpi = GetDpi(bitmapDensity); return(BitmapSource.Create(Width, Height, dpi.X, dpi.Y, format, null, bytes, stride)); } } finally { if (!ReferenceEquals(this, image)) { image.Dispose(); } } }