Ejemplo n.º 1
0
 private void SetBitmapDensity(Bitmap bitmap, BitmapDensity bitmapDensity)
 {
     if (bitmapDensity == BitmapDensity.Use)
     {
         var dpi = GetDpi(bitmapDensity);
         bitmap.SetResolution((float)dpi.X, (float)dpi.Y);
     }
 }
Ejemplo n.º 2
0
 private static void SetBitmapDensity(MagickImage imageMagick, Bitmap bitmap, BitmapDensity bitmapDensity)
 {
     if (bitmapDensity == BitmapDensity.Use)
     {
         var dpi = GetDpi(imageMagick, bitmapDensity);
         bitmap.SetResolution((float)dpi.X, (float)dpi.Y);
     }
 }
Ejemplo n.º 3
0
        private Density GetDpi(BitmapDensity bitmapDensity)
        {
            if (bitmapDensity == BitmapDensity.Ignore || (Density.Units == DensityUnit.Undefined && Density.X == 0 && Density.Y == 0))
            {
                return(new Density(96));
            }

            return(Density.ChangeUnits(DensityUnit.PixelsPerInch));
        }
Ejemplo n.º 4
0
        private static Density GetDpi(MagickImage imageMagick, BitmapDensity bitmapDensity)
        {
            if (bitmapDensity == BitmapDensity.Ignore || (imageMagick.Density.Units == DensityUnit.Undefined && imageMagick.Density.X == 0 && imageMagick.Density.Y == 0))
            {
                return(new Density(96));
            }

            return(imageMagick.Density.ChangeUnits(DensityUnit.PixelsPerInch));
        }
Ejemplo n.º 5
0
        public static Bitmap ToBitmap(this MagickImage magickImg, BitmapDensity density)
        {
            string mapping = "BGR";
            var    format  = PixelFormat.Format24bppRgb;

            var image = magickImg;

            try
            {
                if (image.ColorSpace != ColorSpace.sRGB)
                {
                    image            = (MagickImage)magickImg.Clone();
                    image.ColorSpace = ColorSpace.sRGB;
                }

                if (image.HasAlpha)
                {
                    mapping = "BGRA";
                    format  = PixelFormat.Format32bppArgb;
                }

                using (var 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 < image.Height; y++)
                    {
                        byte[] bytes = pixels.ToByteArray(0, y, image.Width, 1, mapping);
                        Marshal.Copy(bytes, 0, destination, bytes.Length);

                        destination = new IntPtr(destination.ToInt64() + data.Stride);
                    }

                    bitmap.UnlockBits(data);
                    SetBitmapDensity(magickImg, bitmap, density);

                    return(bitmap);
                }
            }
            finally
            {
                if (!ReferenceEquals(image, magickImg))
                {
                    image.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts this instance to a <see cref="Bitmap"/> using <see cref="ImageFormat.Bmp"/>.
        /// </summary>
        /// <param name="bitmapDensity">The bitmap density.</param>
        /// <returns>A <see cref="Bitmap"/> that has the format <see cref="ImageFormat.Bmp"/>.</returns>
        public Bitmap ToBitmap(BitmapDensity bitmapDensity)
        {
            IMagickImage image = this;

            string      mapping = "BGR";
            PixelFormat 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())
                {
                    Bitmap     bitmap      = new Bitmap(image.Width, image.Height, format);
                    BitmapData data        = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.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);

                    SetBitmapDensity(bitmap, bitmapDensity);
                    return(bitmap);
                }
            }
            finally
            {
                if (!ReferenceEquals(this, image))
                {
                    image.Dispose();
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Converts this instance to a <see cref="Bitmap"/> using the specified <see cref="ImageFormat"/>.
        /// Supported formats are: Bmp, Gif, Icon, Jpeg, Png, Tiff.
        /// </summary>
        /// <param name="imageFormat">The image format.</param>
        /// <param name="bitmapDensity">The bitmap density.</param>
        /// <returns>A <see cref="Bitmap"/> that has the specified <see cref="ImageFormat"/>.</returns>
        public Bitmap ToBitmap(ImageFormat imageFormat, BitmapDensity bitmapDensity)
        {
            Format = MagickFormatInfo.GetFormat(imageFormat);

            MemoryStream memStream = new MemoryStream();

            Write(memStream);
            memStream.Position = 0;

            /* Do not dispose the memStream, the bitmap owns it. */
            var bitmap = new Bitmap(memStream);

            SetBitmapDensity(bitmap, bitmapDensity);

            return(bitmap);
        }
Ejemplo n.º 8
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();
                }
            }
        }
Ejemplo n.º 9
0
        public static Bitmap ToBitmap(this MagickImage imageMagick, ImageFormat imageFormat, BitmapDensity bitmapDensity)
        {
            imageMagick.Format = InternalMagickFormatInfo.GetFormat(imageFormat);

            MemoryStream memStream = new MemoryStream();

            imageMagick.Write(memStream);
            memStream.Position = 0;

            /* Do not dispose the memStream, the bitmap owns it. */
            var bitmap = new Bitmap(memStream);

            SetBitmapDensity(imageMagick, bitmap, bitmapDensity);

            return(bitmap);
        }