Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 4
0
        /// <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);
            }
        }
Exemplo n.º 5
0
        /// <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));
            }
        }
Exemplo n.º 6
0
 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);
         }
     }
 }
Exemplo n.º 7
0
 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");
                 });
             }
         }
     }
 }
Exemplo n.º 8
0
 public void ShouldReturnNullWhenGeometryIsNull()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             var values = pixels.ToByteArray(null, "RGB");
             Assert.IsNull(values);
         }
     }
 }
Exemplo n.º 9
0
            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);
                    }
                }
            }
Exemplo n.º 10
0
 public void ShouldThrowExceptionWhenMappingIsEmpty()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.Throws <MagickResourceLimitErrorException>(() =>
             {
                 pixels.ToByteArray(string.Empty);
             });
         }
     }
 }
Exemplo n.º 11
0
            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);
                    }
                }
            }
Exemplo n.º 12
0
 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);
             });
         }
     }
 }
Exemplo n.º 13
0
 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);
             });
         }
     }
 }
Exemplo n.º 14
0
 public void ToByteArray_MappingIsEmpty_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.ThrowsArgumentException("mapping", () =>
             {
                 pixels.ToByteArray(string.Empty);
             });
         }
     }
 }
Exemplo n.º 15
0
        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);
                }
            }
        }
Exemplo n.º 16
0
 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);
             });
         }
     }
 }
Exemplo n.º 17
0
            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);
                    }
                }
            }
Exemplo n.º 18
0
 public void ShouldThrowExceptionWhenGeometryIsNullAndMappingIsEnum()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.Throws <ArgumentNullException>("geometry", () =>
             {
                 pixels.ToByteArray(null, PixelMapping.RGB);
             });
         }
     }
 }
Exemplo n.º 19
0
            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);
                    }
                }
            }
Exemplo n.º 20
0
 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");
             });
         }
     }
 }
Exemplo n.º 21
0
 public void ShouldThrowExceptionWhenMappingIsEmpty()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.Throws <ArgumentException>("mapping", () =>
             {
                 pixels.ToByteArray(string.Empty);
             });
         }
     }
 }
Exemplo n.º 22
0
 public void ShouldThrowExceptionWhenMappingIsInvalid()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.Throws <MagickOptionErrorException>(() =>
             {
                 pixels.ToByteArray("FOO");
             });
         }
     }
 }
Exemplo n.º 23
0
 public void ShouldThrowExceptionWhenMappingIsNull()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentNullException("mapping", () =>
             {
                 pixels.ToByteArray(null);
             });
         }
     }
 }
Exemplo n.º 24
0
            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);
                    }
                }
            }
Exemplo n.º 25
0
 public void ToByteArray_GeometryIsNull_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentNullException("geometry", () =>
             {
                 pixels.ToByteArray(null, "RGB");
             });
         }
     }
 }
Exemplo n.º 26
0
        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);
                }
            }
        }
Exemplo n.º 27
0
 public void ToByteArray_InvalidMapping_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.Throws<MagickOptionErrorException>(() =>
             {
                 pixels.ToByteArray("FOO");
             });
         }
     }
 }
Exemplo n.º 28
0
        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));
        }
Exemplo n.º 29
0
        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();
                }
            }
        }
Exemplo n.º 30
0
        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();
                }
            }
        }