Пример #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);
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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));
            }
        }
Пример #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);
         }
     }
 }
Пример #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");
                 });
             }
         }
     }
 }
Пример #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);
         }
     }
 }
Пример #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);
                    }
                }
            }
Пример #10
0
 public void ShouldThrowExceptionWhenMappingIsEmpty()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.Throws <MagickResourceLimitErrorException>(() =>
             {
                 pixels.ToByteArray(string.Empty);
             });
         }
     }
 }
Пример #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);
                    }
                }
            }
Пример #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);
             });
         }
     }
 }
Пример #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);
             });
         }
     }
 }
Пример #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);
             });
         }
     }
 }
Пример #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);
                }
            }
        }
Пример #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);
             });
         }
     }
 }
Пример #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);
                    }
                }
            }
Пример #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);
             });
         }
     }
 }
Пример #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);
                    }
                }
            }
Пример #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");
             });
         }
     }
 }
Пример #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);
             });
         }
     }
 }
Пример #22
0
 public void ShouldThrowExceptionWhenMappingIsInvalid()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.Throws <MagickOptionErrorException>(() =>
             {
                 pixels.ToByteArray("FOO");
             });
         }
     }
 }
Пример #23
0
 public void ShouldThrowExceptionWhenMappingIsNull()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentNullException("mapping", () =>
             {
                 pixels.ToByteArray(null);
             });
         }
     }
 }
Пример #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);
                    }
                }
            }
Пример #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");
             });
         }
     }
 }
Пример #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);
                }
            }
        }
Пример #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");
             });
         }
     }
 }
Пример #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));
        }
Пример #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();
                }
            }
        }
Пример #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();
                }
            }
        }