Example #1
0
        private Image CreateBorder(Image sourceImage, int borderSize, Color borderColor, PixelFormat targetPixelformat, out Point offset)
        {
            offset = new Point(borderSize, borderSize);

            Bitmap newImage = ImageOperator.CreateEmptyBitmap(sourceImage.Width + (borderSize * 2), sourceImage.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);

            using (Graphics graphics = Graphics.FromImage(newImage)) {
                // Make sure we draw with the best quality!
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                using (GraphicsPath path = new GraphicsPath()) {
                    path.AddRectangle(new Rectangle(borderSize >> 1, borderSize >> 1, newImage.Width - (borderSize), newImage.Height - (borderSize)));
                    using (Pen pen = new Pen(borderColor, borderSize)) {
                        pen.LineJoin = LineJoin.Round;
                        pen.StartCap = LineCap.Round;
                        pen.EndCap   = LineCap.Round;
                        graphics.DrawPath(pen, path);
                    }
                }
                // draw original with a TextureBrush so we have nice antialiasing!
                using (Brush textureBrush = new TextureBrush(sourceImage, WrapMode.Clamp)) {
                    // We need to do a translate-tranform otherwise the image is wrapped
                    graphics.TranslateTransform(offset.X, offset.Y);
                    graphics.FillRectangle(textureBrush, 0, 0, sourceImage.Width, sourceImage.Height);
                }
            }
            return(newImage);
        }
Example #2
0
        public static IFastBitmap CreateEmpty(Size newSize, PixelFormat pixelFormat, Color backgroundColor)
        {
            Bitmap      destination = ImageOperator.CreateEmptyBitmap(newSize.Width, newSize.Height, pixelFormat, backgroundColor, 96f, 96f);
            IFastBitmap fastBitmap  = Create(destination);

            fastBitmap.NeedsDispose = true;
            return(fastBitmap);
        }
Example #3
0
        public Image Sharpen(Image sourceImage, float depth)
        {
            Bitmap sourceBitmap = sourceImage.Clone() as Bitmap;

            if (sourceBitmap == null)
            {
                return(null);
            }

            Bitmap newImage = ImageOperator.CreateEmptyBitmap(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat,
                                                              Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);

            Rectangle  rect       = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
            BitmapData bitmapdata = sourceBitmap.LockBits(rect, ImageLockMode.ReadWrite, sourceBitmap.PixelFormat);

            int bytePerPixelColor = GetBytePerPixelColoer(bitmapdata.PixelFormat);

            if (bytePerPixelColor != 3 && bytePerPixelColor != 4)
            {
                log.Error("Pixel format not supported for sharpen:" + bitmapdata.PixelFormat.ToString());
                return(sourceImage);
            }

            //获取像素地址值
            IntPtr ptr = bitmapdata.Scan0;
            //每个图片需要记录长乘以宽*3(RGB,实际顺序应该是BGR)
            int bytes = sourceBitmap.Width * sourceBitmap.Height * bytePerPixelColor;

            byte[] rgbvalues = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes);//Marshal使用
            for (int i = 0; i < sourceBitmap.Width; i++)
            {
                for (int j = 0; j < sourceBitmap.Height; j++)
                {
                    int ltIndex  = ((i == 0 ? i : i - 1) + (j == 0 ? j : j - 1) * sourceBitmap.Width) * bytePerPixelColor;
                    int curIndex = (i + j * sourceBitmap.Width) * bytePerPixelColor;

                    int blueValueLT = rgbvalues[ltIndex];
                    int blueValue   = rgbvalues[curIndex];
                    int newValue    = (int)(blueValue + depth * (blueValue - blueValueLT));
                    if (newValue < 0)
                    {
                        newValue = 0;
                    }
                    if (newValue > 255)
                    {
                        newValue = 255;
                    }
                    rgbvalues[curIndex] = (byte)newValue;

                    int greenValueLT = rgbvalues[ltIndex + 1];
                    int greenValue   = rgbvalues[curIndex + 1];
                    newValue = (int)(greenValue + depth * (greenValue - greenValueLT));
                    if (newValue < 0)
                    {
                        newValue = 0;
                    }
                    if (newValue > 255)
                    {
                        newValue = 255;
                    }
                    rgbvalues[curIndex + 1] = (byte)newValue;

                    int redValueLT = rgbvalues[ltIndex + 2];
                    int redValue   = rgbvalues[curIndex + 2];
                    newValue = (int)(redValue + depth * (redValue - redValueLT));
                    if (newValue < 0)
                    {
                        newValue = 0;
                    }
                    if (newValue > 255)
                    {
                        newValue = 255;
                    }
                    rgbvalues[curIndex + 2] = (byte)newValue;
                }
            }
            sourceBitmap.UnlockBits(bitmapdata);

            BitmapData newBitmapdata = newImage.LockBits(rect, ImageLockMode.ReadWrite, sourceBitmap.PixelFormat);

            System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, newBitmapdata.Scan0, bytes);//Marshal
            newImage.UnlockBits(newBitmapdata);

            return(newImage);
        }
Example #4
0
        // 由指定bitmap创建一个新有带阴影的bitmap
        protected Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point shadowOffset,
                                      out Point offset, PixelFormat targetPixelformat)
        {
            // Create a new "clean" image
            offset    = shadowOffset;
            offset.X += shadowSize - 1;
            offset.Y += shadowSize - 1;
            Bitmap returnImage = ImageOperator.CreateEmptyBitmap(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2),
                                                                 targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);

            // Make sure the shadow is odd, there is no reason for an even blur!
            if ((shadowSize & 1) == 0)
            {
                shadowSize++;
            }
            bool useGDIBlur = GDIplus.IsBlurPossible(shadowSize);
            // Create "mask" for the shadow
            ColorMatrix maskMatrix = new ColorMatrix();

            maskMatrix.Matrix00 = 0;
            maskMatrix.Matrix11 = 0;
            maskMatrix.Matrix22 = 0;
            if (useGDIBlur)
            {
                maskMatrix.Matrix33 = darkness + 0.1f;
            }
            else
            {
                maskMatrix.Matrix33 = darkness;
            }
            Rectangle shadowRectangle = new Rectangle(new Point(shadowSize, shadowSize), sourceBitmap.Size);

            ImageOperator.ApplyColorMatrix((Bitmap)sourceBitmap, Rectangle.Empty, returnImage, shadowRectangle, maskMatrix);

            // blur "shadow", apply to whole new image
            if (useGDIBlur)
            {
                // Use GDI Blur
                Rectangle newImageRectangle = new Rectangle(0, 0, returnImage.Width, returnImage.Height);
                if (!GDIplus.ApplyBlur(returnImage, newImageRectangle, shadowSize + 1, false))
                {
                    log.Error("Failed to ApplyBlur in GDIplus.");
                }
            }
            else
            {
                // try normal software blur
                ApplyBoxBlur(returnImage, shadowSize);
            }

            // Draw the original image over the shadow
            using (Graphics graphics = Graphics.FromImage(returnImage)) {
                // Make sure we draw with the best quality!
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                // draw original with a TextureBrush so we have nice antialiasing!
                using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
                    // We need to do a translate-tranform otherwise the image is wrapped
                    graphics.TranslateTransform(offset.X, offset.Y);
                    graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
                }
            }
            return(returnImage);
        }
Example #5
0
        private Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange)
        {
            Image returnImage = ImageOperator.CreateEmptyBitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);

            using (GraphicsPath path = new GraphicsPath()) {
                Random random            = new Random();
                int    HorizontalRegions = (int)(sourceImage.Width / horizontalToothRange);
                int    VerticalRegions   = (int)(sourceImage.Height / verticalToothRange);

                // Start
                Point previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
                Point newEndingPoint;
                // Top
                for (int i = 0; i < HorizontalRegions; i++)
                {
                    int x = (int)previousEndingPoint.X + horizontalToothRange;
                    int y = random.Next(1, toothHeight);
                    newEndingPoint = new Point(x, y);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                // Right
                for (int i = 0; i < VerticalRegions; i++)
                {
                    int x = sourceImage.Width - random.Next(1, toothHeight);
                    int y = (int)previousEndingPoint.Y + verticalToothRange;
                    newEndingPoint = new Point(x, y);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                // Bottom
                for (int i = 0; i < HorizontalRegions; i++)
                {
                    int x = (int)previousEndingPoint.X - horizontalToothRange;
                    int y = sourceImage.Height - random.Next(1, toothHeight);
                    newEndingPoint = new Point(x, y);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }

                // Left
                for (int i = 0; i < VerticalRegions; i++)
                {
                    int x = random.Next(1, toothHeight);
                    int y = (int)previousEndingPoint.Y - verticalToothRange;
                    newEndingPoint = new Point(x, y);
                    path.AddLine(previousEndingPoint, newEndingPoint);
                    previousEndingPoint = newEndingPoint;
                }
                path.CloseFigure();

                // Draw the created figure with the original image by using a TextureBrush so we have anti-aliasing
                using (Graphics graphics = Graphics.FromImage(returnImage)) {
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    using (Brush brush = new TextureBrush(sourceImage)) {
                        // Imporant note: If the target wouldn't be at 0,0 we need to translate-transform!!
                        graphics.FillPath(brush, path);
                    }
                }
            }
            return(returnImage);
        }