Example #1
0
 public static void ResizeImage(Stream input, Stream output)
 {
     var originalImage = new Bitmap(input);
     const int width = 300;
     var height = ((double)originalImage.Height / originalImage.Width) * width;
     Bitmap scaledImage = null;
     try
     {
         scaledImage = new Bitmap(width, (int)height);
         using (var graphics = Graphics.FromImage(scaledImage))
         {
             graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
             graphics.SmoothingMode = SmoothingMode.AntiAlias;
             graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
             graphics.DrawImage(originalImage, 0, 0, width, (int)height);
         }
         scaledImage.Save(output, ImageFormat.Jpeg);
     }
     finally
     {
         scaledImage?.Dispose();
     }
 }
Example #2
0
        /// <summary>
        /// Draws the overlay icon.
        /// </summary>
        private void DrawOverlayIcon()
        {
            Bitmap overlayIcon = null;
            try
            {
                switch (m_item.MetaGroup)
                {
                    case ItemMetaGroup.T2:
                        overlayIcon = Properties.Resources.T2;
                        break;
                    case ItemMetaGroup.T3:
                        overlayIcon = Properties.Resources.T3;
                        break;
                    case ItemMetaGroup.Storyline:
                        overlayIcon = Properties.Resources.Storyline;
                        break;
                    case ItemMetaGroup.Deadspace:
                        overlayIcon = Properties.Resources.Deadspace;
                        break;
                    case ItemMetaGroup.Officer:
                        overlayIcon = Properties.Resources.Officer;
                        break;
                    case ItemMetaGroup.Faction:
                        overlayIcon = Properties.Resources.Faction;
                        break;
                    default:
                        overlayIcon = new Bitmap(16, 16);
                        break;
                }

                Image image = (Image)pbImage.Image.Clone();

                using (Graphics graph = Graphics.FromImage(image))
                {
                    graph.DrawImage(overlayIcon, 0, 0, (int)m_imageSize / 4, (int)m_imageSize / 4);
                }

                pbImage.Image = image;
            }
            finally
            {
                overlayIcon?.Dispose();
            }
        }
Example #3
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Image image = factory.Image;
            int width = image.Width;
            int height = image.Height;
            Bitmap newImage = null;
            Bitmap edgeBitmap = null;
            try
            {
                HalftoneFilter filter = new HalftoneFilter(5);
                newImage = new Bitmap(image);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                newImage = filter.ApplyFilter(newImage);
                bool comicMode = this.DynamicParameter;

                if (comicMode)
                {
                    // Draw the edges.
                    edgeBitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                    edgeBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    edgeBitmap = Effects.Trace(image, edgeBitmap, 120);

                    using (Graphics graphics = Graphics.FromImage(newImage))
                    {
                        // Overlay the image.
                        graphics.DrawImage(edgeBitmap, 0, 0);
                        Rectangle rectangle = new Rectangle(0, 0, width, height);

                        // Draw an edge around the image.
                        using (Pen blackPen = new Pen(Color.Black))
                        {
                            blackPen.Width = 4;
                            graphics.DrawRectangle(blackPen, rectangle);
                        }
                    }

                    edgeBitmap.Dispose();
                }

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                edgeBitmap?.Dispose();

                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Example #4
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                Color tintColour = this.DynamicParameter;
                float[][] colorMatrixElements =
                    {
                        new[] { tintColour.R / 255f, 0, 0, 0, 0 }, // Red
                        new[] { 0, tintColour.G / 255f, 0, 0, 0 }, // Green
                        new[] { 0, 0, tintColour.B / 255f, 0, 0 }, // Blue
                        new[] { 0, 0, 0, tintColour.A / 255f, 0 }, // Alpha
                        new float[] { 0, 0, 0, 0, 1 }
                    };

                ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
                newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;

                    using (ImageAttributes attributes = new ImageAttributes())
                    {
                        attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                        graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);

                        image.Dispose();
                        image = newImage;
                    }
                }
            }
            catch (Exception ex)
            {
                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Bitmap grey = null;
            Image image = factory.Image;
            byte threshold = this.DynamicParameter;

            try
            {
                // Detect the edges then strip out middle shades.
                grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter(image);
                grey = new BinaryThreshold(threshold).ProcessFilter(grey);

                // Search for the first white pixels
                Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(grey, 0);
                grey.Dispose();

                newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    graphics.DrawImage(
                                     image,
                                     new Rectangle(0, 0, rectangle.Width, rectangle.Height),
                                     rectangle.X,
                                     rectangle.Y,
                                     rectangle.Width,
                                     rectangle.Height,
                                     GraphicsUnit.Pixel);
                }

                // Reassign the image.
                image.Dispose();
                image = newImage;

                if (factory.PreserveExifData && factory.ExifPropertyItems.Any())
                {
                    // Set the width EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageWidth, (ushort)image.Width);

                    // Set the height EXIF data.
                    factory.SetPropertyItem(ExifPropertyTag.ImageHeight, (ushort)image.Height);
                }
            }
            catch (Exception ex)
            {
                grey?.Dispose();

                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Example #6
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                // TODO: Optimize this one day when I can break the API.
                newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                IMatrixFilter matrix = this.DynamicParameter;
                newImage = matrix.TransformImage(image, newImage);

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">The current instance of the 
        /// <see cref="T:ImageProcessor.ImageFactory" /> class containing
        /// the image to process.</param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                int width = image.Width;
                int height = image.Height;

                Color backgroundColor = this.DynamicParameter;
                newImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                // Make a graphics object from the empty bitmap.
                using (Graphics graphics = Graphics.FromImage(newImage))
                {
                    // Fill the background.
                    graphics.Clear(backgroundColor);

                    // Draw passed in image onto graphics object.
                    graphics.DrawImage(image, 0, 0, width, height);
                }

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Example #8
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Bitmap mask = null;
            Bitmap maskCropped = null;
            Bitmap maskPositioned = null;
            Image image = factory.Image;

            try
            {
                int width = image.Width;
                int height = image.Height;
                Tuple<Image, Point?> parameters = this.DynamicParameter;
                mask = new Bitmap(parameters.Item1);
                mask.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                Point? position = parameters.Item2;

                if (mask.Size != image.Size)
                {
                    Rectangle parent = new Rectangle(0, 0, width, height);
                    Rectangle child = ImageMaths.GetFilteredBoundingRectangle(mask, 0, RgbaComponent.A);
                    maskCropped = new Bitmap(child.Width, child.Height, PixelFormat.Format32bppPArgb);
                    maskCropped.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                    // First crop any bounding transparency.
                    using (Graphics graphics = Graphics.FromImage(maskCropped))
                    {
                        GraphicsHelper.SetGraphicsOptions(graphics);

                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(
                                         mask,
                                         new Rectangle(0, 0, child.Width, child.Height),
                                         child.X,
                                         child.Y,
                                         child.Width,
                                         child.Height,
                                         GraphicsUnit.Pixel);
                    }

                    // Now position the mask in an image of the same dimensions as the original.
                    maskPositioned = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
                    maskPositioned.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                    using (Graphics graphics = Graphics.FromImage(maskPositioned))
                    {
                        GraphicsHelper.SetGraphicsOptions(graphics, true);
                        graphics.Clear(Color.Transparent);

                        if (position != null)
                        {
                            // Apply the mask at the given position.
                            graphics.DrawImage(maskCropped, position.Value);
                        }
                        else
                        {
                            // Center it instead
                            RectangleF centered = ImageMaths.CenteredRectangle(parent, child);
                            graphics.DrawImage(maskCropped, new PointF(centered.X, centered.Y));
                        }
                    }

                    newImage = Effects.ApplyMask(image, maskPositioned);
                    maskCropped.Dispose();
                    maskPositioned.Dispose();
                }
                else
                {
                    newImage = Effects.ApplyMask(image, mask);
                    mask.Dispose();
                }

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                mask?.Dispose();

                maskCropped?.Dispose();

                maskPositioned?.Dispose();

                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
Example #9
0
        public Image CheckValidImage(Image img, int width, int height)
        {
            if (img == null) return null;

            Bitmap bmp = null;

            try
            {
                bmp = new Bitmap(width, height);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.DrawImage(img, 0, 0, width, height);
                }
                bmp.Tag = img.Tag;

                Bitmap result = bmp;
                bmp = null; //返り値のBitmapはDisposeしない
                return result;
            }
            catch (Exception)
            {
                bmp?.Dispose();

                bmp = new Bitmap(width, height);
                bmp.Tag = img.Tag;

                Bitmap result = bmp;
                bmp = null; //返り値のBitmapはDisposeしない
                return result;
            }
            finally
            {
                bmp?.Dispose();
                img.Dispose();
            }
        }
Example #10
0
        public void ExportThread()
        {
            var zWait = WaitDialog.Instance;

            #if !MONO_BUILD
            Bitmap zBuffer = null;
            #endif

            zWait.ProgressReset(0, 0, ExportLayoutEndIndex - ExportLayoutStartIndex, 0);
            for (var nIdx = ExportLayoutStartIndex; nIdx < ExportLayoutEndIndex; nIdx++)
            {
                ChangeExportLayoutIndex(nIdx);
                zWait.ProgressReset(1, 0, CurrentDeck.CardCount, 0);

                ConfigurePointSizes(CurrentDeck.CardLayout);

                if (0 < nIdx)
                {
                    if (CardMakerSettings.PrintLayoutsOnNewPage)
                    {
                        AddPage();
                    }

                    if (CardMakerSettings.PrintAutoHorizontalCenter ||
                        (m_dDrawX + m_dLayoutPointWidth > m_dMarginEndX)) // this is the case where a layout won't fit in the remaining space of the row
                    {
                        MoveToNextRow();
                    }
                }

                // should be adjusted after the above move to next row
                m_dNextRowYAdjust = Math.Max(m_dNextRowYAdjust, m_dLayoutPointHeight + m_dBufferY);

                if (CardMakerSettings.PrintAutoHorizontalCenter)
                {
                    CenterLayoutPositionOnNewRow();
                }

            #if !MONO_BUILD
                zBuffer?.Dispose();
                zBuffer = new Bitmap(CurrentDeck.CardLayout.width, CurrentDeck.CardLayout.height);

                float fOriginalXDpi = zBuffer.HorizontalResolution;
                float fOriginalYDpi = zBuffer.VerticalResolution;
            #endif

                foreach (var nCardIdx in GetExportIndices())
                {
                    CurrentDeck.ResetDeckCache();

                    CurrentDeck.CardPrintIndex = nCardIdx;

            #if MONO_BUILD
                    // mono build won't support the optimization so re-create the buffer
                    Bitmap zBuffer = new Bitmap(CurrentDeck.CardLayout.width, CurrentDeck.CardLayout.height);
            #endif

            #if !MONO_BUILD
                    // minor optimization, reuse the same bitmap (for drawing sake the DPI has to be reset)
                    zBuffer.SetResolution(fOriginalXDpi, fOriginalYDpi);
            #endif
                    if (nCardIdx == -1)
                    {
                        Graphics.FromImage(zBuffer).FillRectangle(Brushes.White, 0, 0, zBuffer.Width, zBuffer.Height);
                        // note: some oddities were observed where the buffer was not flood filling
                    }
                    else
                    {
                        CardRenderer.DrawPrintLineToGraphics(Graphics.FromImage(zBuffer));
                    }

                    // apply any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, false);

                    // before rendering to the PDF bump the DPI to the desired value
                    zBuffer.SetResolution(CurrentDeck.CardLayout.dpi, CurrentDeck.CardLayout.dpi);

                    var xImage = XImage.FromGdiPlusImage(zBuffer);

                    EvaluatePagePosition();

                    m_zPageGfx.DrawImage(xImage, m_dDrawX, m_dDrawY);

                    MoveToNextColumnPosition();

                    // undo any export rotation
                    ProcessRotateExport(zBuffer, CurrentDeck.CardLayout, true);

                    zWait.ProgressStep(1);
                }
                zWait.ProgressStep(0);
            }

            #if !MONO_BUILD
            zBuffer?.Dispose();
            #endif

            try
            {
                m_zDocument.Save(m_sExportFile);
                zWait.ThreadSuccess = true;
            }
            catch (Exception ex)
            {
                Logger.AddLogLine("Error saving PDF (is it open?) " + ex.Message);
                zWait.ThreadSuccess = false;
            }

            zWait.CloseWaitDialog();
        }
Example #11
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">
        /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
        /// the image to process.
        /// </param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Image image = factory.Image;

            try
            {
                Tuple<Color, Color, int> parameters = this.DynamicParameter;
                Color original = parameters.Item1;
                Color replacement = parameters.Item2;

                byte originalR = original.R;
                byte originalG = original.G;
                byte originalB = original.B;
                byte originalA = original.A;

                byte replacementR = replacement.R;
                byte replacementG = replacement.G;
                byte replacementB = replacement.B;
                byte replacementA = replacement.A;

                int fuzziness = parameters.Item3;

                byte minR = (originalR - fuzziness).ToByte();
                byte minG = (originalG - fuzziness).ToByte();
                byte minB = (originalB - fuzziness).ToByte();

                byte maxR = (originalR + fuzziness).ToByte();
                byte maxG = (originalG + fuzziness).ToByte();
                byte maxB = (originalB + fuzziness).ToByte();

                newImage = new Bitmap(image);
                newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                int width = image.Width;
                int height = image.Height;

                using (FastBitmap fastBitmap = new FastBitmap(newImage))
                {
                    Parallel.For(
                        0,
                        height,
                        y =>
                        {
                            for (int x = 0; x < width; x++)
                            {
                                // Get the pixel color.
                                // ReSharper disable once AccessToDisposedClosure
                                Color currentColor = fastBitmap.GetPixel(x, y);

                                byte currentR = currentColor.R;
                                byte currentG = currentColor.G;
                                byte currentB = currentColor.B;
                                byte currentA = currentColor.A;

                                // Test whether it is in the expected range.
                                if (ImageMaths.InRange(currentR, minR, maxR))
                                {
                                    if (ImageMaths.InRange(currentG, minG, maxG))
                                    {
                                        if (ImageMaths.InRange(currentB, minB, maxB))
                                        {
                                            // Ensure the values are within an acceptable byte range
                                            // and set the new value.
                                            byte r = (originalR - currentR + replacementR).ToByte();
                                            byte g = (originalG - currentG + replacementG).ToByte();
                                            byte b = (originalB - currentB + replacementB).ToByte();

                                            // Allow replacement with transparent color.
                                            byte a = currentA;
                                            if (originalA != replacementA)
                                            {
                                                a = replacementA;
                                            }

                                            // ReSharper disable once AccessToDisposedClosure
                                            fastBitmap.SetPixel(x, y, Color.FromArgb(a, r, g, b));
                                        }
                                    }
                                }
                            }
                        });
                }

                image.Dispose();
                image = newImage;
            }
            catch (Exception ex)
            {
                newImage?.Dispose();

                throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
            }

            return image;
        }
        public Bitmap CreateImage(string checkCode)
        {
            Bitmap image = default(Bitmap);
            Graphics g = default(Graphics);
            try
            {
                int iwidth = (int)(checkCode.Length * 11);
                image = new Bitmap(iwidth, 19);
                g = Graphics.FromImage(image);
                g.Clear(Color.White);

                Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Chocolate, Color.Brown, Color.DarkCyan, Color.Purple };
                Random rand = new Random();

                for (int i = 0; i < checkCode.Length; i++)
                {
                    int cindex = rand.Next(7);
                    Font f = new Font("Microsoft Sans Serif", 11);
                    Brush b = new SolidBrush(c[cindex]);
                    g.DrawString(checkCode.Substring(i, 1), f, b, (i * 10) + 1, 0, StringFormat.GenericDefault);
                }

                g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                //Response.ClearContent();
                //Response.ContentType = "image/Jpeg";
                //Response.BinaryWrite(ms.ToArray());

                return image;
            }
            finally
            {
                g?.Dispose();
                image?.Dispose();
            }




        }