Example #1
0
        internal static void DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, bool rightToLeft)
        {
            if (backgroundImageLayout == ImageLayout.Tile)
            {
                using (TextureBrush brush1 = new TextureBrush(backgroundImage, WrapMode.Tile))
                {
                    if (scrollOffset != Point.Empty)
                    {
                        Matrix matrix1 = brush1.Transform;
                        matrix1.Translate((float)scrollOffset.X, (float)scrollOffset.Y);
                        brush1.Transform = matrix1;
                    }
                    g.FillRectangle(brush1, clipRect);
                    return;
                }
            }
            Rectangle rectangle1 = CalculateBackgroundImageRectangle(bounds, backgroundImage, backgroundImageLayout);

            if ((rightToLeft == true) && (backgroundImageLayout == ImageLayout.None))
            {
                rectangle1.X += clipRect.Width - rectangle1.Width;
            }
            using (SolidBrush brush2 = new SolidBrush(backColor))
            {
                g.FillRectangle(brush2, clipRect);
            }
            if (!clipRect.Contains(rectangle1))
            {
                if ((backgroundImageLayout == ImageLayout.Stretch) || (backgroundImageLayout == ImageLayout.Zoom))
                {
                    rectangle1.Intersect(clipRect);
                    g.DrawImage(backgroundImage, rectangle1);
                }
                else if (backgroundImageLayout == ImageLayout.None)
                {
                    rectangle1.Offset(clipRect.Location);
                    Rectangle rectangle2 = rectangle1;
                    rectangle2.Intersect(clipRect);
                    Rectangle rectangle3 = new Rectangle(Point.Empty, rectangle2.Size);
                    g.DrawImage(backgroundImage, rectangle2, rectangle3.X, rectangle3.Y, rectangle3.Width, rectangle3.Height, GraphicsUnit.Pixel);
                }
                else
                {
                    Rectangle rectangle4 = rectangle1;
                    rectangle4.Intersect(clipRect);
                    Rectangle rectangle5 = new Rectangle(new Point(rectangle4.X - rectangle1.X, rectangle4.Y - rectangle1.Y), rectangle4.Size);
                    g.DrawImage(backgroundImage, rectangle4, rectangle5.X, rectangle5.Y, rectangle5.Width, rectangle5.Height, GraphicsUnit.Pixel);
                }
            }
            else
            {
                ImageAttributes attributes1 = new ImageAttributes();
                attributes1.SetWrapMode(WrapMode.TileFlipXY);
                g.DrawImage(backgroundImage, rectangle1, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, attributes1);
                attributes1.Dispose();
            }
        }
Example #2
0
        public void displayScreenshotOnDevice(Point capturePoint, Screen captureScreen)
        {
            if (InitOk)
            {
                // 320x240 default logitech/saitek fip resolution

                // resize if size has changed
                if (Properties.Settings.Default.CaptureWidth != 320 || Properties.Settings.Default.CaptureHeight != 240)
                {
                    Bitmap captureBitmap = new Bitmap(Properties.Settings.Default.CaptureWidth, Properties.Settings.Default.CaptureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    Rectangle captureRectangle = captureScreen.Bounds;

                    Graphics captureGraphics = Graphics.FromImage(captureBitmap);

                    captureGraphics.CopyFromScreen(capturePoint.X, capturePoint.Y, 0, 0, captureRectangle.Size);
                    Rectangle resizedRect   = new Rectangle(0, 0, 320, 240);
                    Bitmap    resizedBitmap = new Bitmap(320, 240);

                    resizedBitmap.SetResolution(captureBitmap.HorizontalResolution, captureBitmap.VerticalResolution);
                    resizedBitmap.SetResolution(320, 240);

                    Graphics resizedGraphics = Graphics.FromImage(resizedBitmap);
                    resizedGraphics.CompositingMode    = CompositingMode.SourceCopy;
                    resizedGraphics.CompositingQuality = CompositingQuality.HighQuality;
                    resizedGraphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    resizedGraphics.SmoothingMode      = SmoothingMode.HighQuality;
                    resizedGraphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                    ImageAttributes wrapMode = new ImageAttributes();
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    resizedGraphics.DrawImage(captureBitmap, resizedRect, 0, 0, captureBitmap.Width, captureBitmap.Height, GraphicsUnit.Pixel, wrapMode);
                    captureBitmap.Dispose();
                    captureBitmap = new Bitmap(resizedBitmap);
                    resizedGraphics.Dispose();
                    captureGraphics.Dispose();
                    wrapMode.Dispose();
                    _fipPanels[0].SendImageToFip(0, captureBitmap);
                    resizedBitmap.Dispose();
                    captureBitmap.Dispose();
                }
                else // if default resolution
                {
                    Bitmap captureBitmap = new Bitmap(Properties.Settings.Default.CaptureWidth, Properties.Settings.Default.CaptureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                    Rectangle captureRectangle = captureScreen.Bounds;

                    Graphics captureGraphics = Graphics.FromImage(captureBitmap);

                    captureGraphics.CopyFromScreen(capturePoint.X, capturePoint.Y, 0, 0, captureRectangle.Size);
                    _fipPanels[0].SendImageToFip(0, captureBitmap);
                    captureGraphics.Dispose();
                    captureBitmap.Dispose();
                }
            }
        }
Example #3
0
        /// <summary>
        /// 加水印图片
        /// </summary>
        /// <param name="picture">imge 对象</param>
        /// <param name="_width">原始图片的宽度</param>
        /// <param name="_height">原始图片的高度.</param>
        private void addWatermarkImage(Graphics picture, int _width, int _height, HTProject_Bizlogic.CommonEnum.WaterMarkPosition Pos)
        {
            if (!System.IO.File.Exists(this.Page.MapPath(this.WatermarkImageUrl)))
            {
                return;
            }
            System.Drawing.Image watermark = new Bitmap(this.Page.MapPath(this.WatermarkImageUrl));

            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap        colorMap        = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int    xpos            = 0;
            int    ypos            = 0;
            int    WatermarkWidth  = 0;
            int    WatermarkHeight = 0;
            double bl = 1d;


            WatermarkWidth  = watermark.Width;  //Convert.ToInt32(_width);
            WatermarkHeight = watermark.Height; // Convert.ToInt32(_height);


            //picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            //循环加上水印
            for (int y = 0; y < _height; y = y + WatermarkHeight * 2)
            {
                for (int x = 0; x < _width; x = x + WatermarkWidth * 2)
                {
                    xpos = x;
                    ypos = y;
                    picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                }
            }

            watermark.Dispose();
            imageAttributes.Dispose();
        }
        /// <summary>
        /// 图片水印
        /// </summary>
        /// <param name="destBitmap"></param>
        /// <param name="watermarkBitmap"></param>
        /// <param name="currentSettings"></param>
        private void PlaceImageWatermark(Bitmap destBitmap, Bitmap watermarkBitmap, WatermarkSettings currentSettings)
        {
            currentSettings = currentSettings ?? new WatermarkSettings();
            using (Graphics g = Graphics.FromImage(destBitmap))
            {
                Bitmap rotateImage = null;
                if (currentSettings.ImageRotatedDegree > 0)
                {
                    rotateImage = GetRotateImage(watermarkBitmap, currentSettings.ImageRotatedDegree);
                }
                if (rotateImage != null)
                {
                    watermarkBitmap = rotateImage;
                }
                double watermarkSizeInPercent = (double)currentSettings.PictureSettings.Size / 100;

                Size boundingBoxSize = new Size((int)(destBitmap.Width * watermarkSizeInPercent),
                                                (int)(destBitmap.Height * watermarkSizeInPercent));
                Size calculatedWatermarkSize = ScaleRectangleToFitBounds(boundingBoxSize, watermarkBitmap.Size);

                if (calculatedWatermarkSize.Width == 0 || calculatedWatermarkSize.Height == 0)
                {
                    return;
                }

                Bitmap scaledWatermarkBitmap =
                    new Bitmap(calculatedWatermarkSize.Width, calculatedWatermarkSize.Height);
                using (Graphics watermarkGraphics = Graphics.FromImage(scaledWatermarkBitmap))
                {
                    ColorMatrix opacityMatrix = new ColorMatrix
                    {
                        Matrix33 = (float)currentSettings.PictureSettings.Opacity
                    };
                    ImageAttributes attrs = new ImageAttributes();
                    attrs.SetColorMatrix(opacityMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    watermarkGraphics.DrawImage(watermarkBitmap,
                                                new Rectangle(0, 0, scaledWatermarkBitmap.Width, scaledWatermarkBitmap.Height),
                                                0, 0, watermarkBitmap.Width, watermarkBitmap.Height,
                                                GraphicsUnit.Pixel, attrs);
                    attrs.Dispose();
                }

                foreach (var position in currentSettings.PictureSettings.PositionList)
                {
                    Point watermarkPosition = CalculateWatermarkPosition(position,
                                                                         destBitmap.Size, calculatedWatermarkSize);

                    g.DrawImage(scaledWatermarkBitmap,
                                new Rectangle(watermarkPosition, calculatedWatermarkSize),
                                0, 0, calculatedWatermarkSize.Width, calculatedWatermarkSize.Height, GraphicsUnit.Pixel);
                }
                scaledWatermarkBitmap.Dispose();
            }
        }
Example #5
0
        public void DrawImageWithTransparency(Graphics G, float alpha, Image image, Rectangle rect)
        {
            ColorMatrix colorMatrix = new ColorMatrix {
                Matrix33 = alpha
            };
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetColorMatrix(colorMatrix);
            G.DrawImage(image, new Rectangle(rect.X, rect.Y, image.Width, image.Height), rect.X, rect.Y, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            imageAttributes.Dispose();
        }
Example #6
0
        public static void DrawTransparentImage(this Graphics g, float alpha, Image image, Rectangle rect)
        {
            var colorMatrix = new ColorMatrix {
                Matrix33 = alpha
            };
            var imageAttributes = new ImageAttributes();

            imageAttributes.SetColorMatrix(colorMatrix);
            g.DrawImage(image, new Rectangle(rect.X, rect.Y, image.Width, image.Height), rect.X, rect.Y, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            imageAttributes.Dispose();
        }
Example #7
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.Windows.Forms.TreeView" /> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_linkAttributes != null)
                    {
                        _linkAttributes.Dispose();
                        _linkAttributes = null;
                    }

                    if (_fadeAttributes != null)
                    {
                        _fadeAttributes.Dispose();
                        _fadeAttributes = null;
                    }

                    if (_renameBox != null)
                    {
                        _renameBox.KeyDown -= _renameBox_KeyDown;
                        _renameBox.Dispose();
                    }

                    if (_selectBrush != null)
                    {
                        _selectBrush.Dispose();
                    }

                    if (_focusPen != null)
                    {
                        _focusPen.Dispose();
                    }

                    if (_excludedContent != null)
                    {
                        _excludedContent.Dispose();
                    }

                    if (_openContent != null)
                    {
                        _openContent.Dispose();
                    }
                }

                _selectBrush     = null;
                _focusPen        = null;
                _excludedContent = null;
                _openContent     = null;
                _disposed        = true;
            }

            base.Dispose(disposing);
        }
Example #8
0
        /// <summary>Draws the image with a custom transparency.</summary>
        /// <param name="graphics">The graphics to draw on.</param>
        /// <param name="alpha">The alpha.</param>
        /// <param name="rectangle">The rectangle.</param>
        /// <param name="image">The image.</param>
        /// <param name="color">The color.</param>
        public static void DrawTransparentImage(Graphics graphics, float alpha, Rectangle rectangle, Image image, Color color)
        {
            ColorMatrix _colorMatrix = new ColorMatrix {
                Matrix33 = alpha
            };
            ImageAttributes _imageAttributes = new ImageAttributes();

            _imageAttributes.SetColorMatrix(_colorMatrix);
            graphics.DrawImage(image, new Rectangle(rectangle.X, rectangle.Y, image.Width, image.Height), rectangle.X, rectangle.Y, image.Width, image.Height, GraphicsUnit.Pixel, _imageAttributes);
            _imageAttributes.Dispose();
        }
Example #9
0
        public static void DrawRoundedRectangleAlpha(this Graphics gx, Color borderColor, Color backColor, Rectangle rc, Size size, byte transparency)
        {
            Pen borderPen = new Pen(borderColor);
            ////Create temp bitmap
            //Bitmap tempBitmap = new Bitmap(rc.Width, rc.Height);
            //Graphics tempGraphics = Graphics.FromImage(tempBitmap);
            //DrawRoundedRect(tempGraphics, borderPen, backColor, new Rectangle(0, 0, rc.Width, rc.Width), size);
            //gx.DrawAlpha(tempBitmap, transparency, rc.X, rc.Y);

            ////Clean up
            //borderPen.Dispose();
            //tempGraphics.Dispose();
            //tempBitmap.Dispose();

            // Prepare image for gradient
            Bitmap roundedImage = new Bitmap(rc.Width, rc.Height);
            // Create temporary graphics
            Graphics gxRounded = Graphics.FromImage(roundedImage);
            // This is our rectangle
            Rectangle roundedRect = new Rectangle(0, 0, rc.Width, rc.Height);

            // Draw rounded rect
            DrawRoundedRect(gxRounded, borderPen, backColor, new Rectangle(0, 0, rc.Width, rc.Width), size);

            // Prepare the copy of the screen graphics
            Bitmap   tempBitmap = new Bitmap(240, 320);
            Graphics tempGx     = Graphics.FromImage(tempBitmap);

            // Copy from the screen's graphics to the temp graphics
            CopyGraphics(gx, tempGx, 240, 320);
            // Draw the gradient image with transparency on the temp graphics
            tempGx.DrawAlpha(roundedImage, transparency, rc.X, rc.Y);
            // Cut out the transparent image
            gxRounded.DrawImage(tempBitmap, new Rectangle(0, 0, rc.Width, rc.Height), rc, GraphicsUnit.Pixel);
            // Prepare for imposing
            roundedRect.Width--;
            roundedRect.Height--;
            // Impose the rounded rectangle with transparent color
            Bitmap borderImage = ImposeRoundedRectangle(roundedRect, borderColor, size);
            // Draw the transparent rounded rectangle
            ImageAttributes attrib = new ImageAttributes();

            attrib.SetColorKey(Color.Green, Color.Green);
            gxRounded.DrawImage(borderImage, new Rectangle(0, 0, rc.Width, rc.Height), 0, 0, borderImage.Width, borderImage.Height, GraphicsUnit.Pixel, attrib);
            // OK... now are ready to draw the final image on the original graphics
            gx.DrawImageTransparent(roundedImage, rc);

            // Clean up
            attrib.Dispose();
            tempGx.Dispose();
            tempBitmap.Dispose();
            roundedImage.Dispose();
            gxRounded.Dispose();
        }
Example #10
0
        /// <summary>
        /// 将水印图片叠加到原先的图片中
        /// </summary>
        /// <param name="gSrcCanvas"></param>
        /// <param name="imgWatermark"></param>
        /// <param name="watermarkPosition"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        private static void AddWatermarkImage(Graphics gSrcCanvas, Image imgWatermark, string watermarkPosition, int width, int height)
        {
            Image           watermark = new Bitmap(imgWatermark);
            ImageAttributes imageAttr = new ImageAttributes();
            ColorMap        colorMap  = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };
            imageAttr.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
            };
            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            int xpos            = 0;
            int ypos            = 0;
            int watermarkWidth  = watermark.Width;
            int watermarkHeight = watermark.Height;

            switch (watermarkPosition)
            {
            case "WM_TOP_LEFT":
                xpos = 10;
                ypos = 10;
                break;

            case "WM_TOP_RIGHT":
                xpos = width - watermarkWidth - 10;
                ypos = 10;
                break;

            case "WM_BOTTOM_RIGHT":
                xpos = width - watermarkWidth - 10;
                ypos = height - watermarkHeight - 10;
                break;

            case "WM_BOTTOM_LEFT":
                xpos = 10;
                ypos = height - watermarkHeight - 10;
                break;
            }
            gSrcCanvas.DrawImage(watermark, new Rectangle(xpos, ypos, watermarkWidth, watermarkHeight), 0, 0,
                                 watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttr);
            gSrcCanvas.Dispose();
            watermark.Dispose();
            imageAttr.Dispose();
        }
Example #11
0
        /// <summary>
        /// 插入水印
        /// </summary>
        /// <param name="image">image对象</param>
        /// <param name="path">路径</param>
        /// <param name="Watermark">水印</param>
        /// <param name="format">格式</param>
        /// <returns></returns>
        public static bool InsertWaterMark(Image image, string path, string Watermark, ImageFormat format)
        {
            if (!File.Exists(Watermark))
            {
                return(false);
            }

            //公司logo
            Image           watermark       = new Bitmap(Watermark);
            Bitmap          b               = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
            ImageAttributes imageAttributes = new ImageAttributes();

            try
            {
                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.White);
                g.DrawImage(image, 0, 0, image.Width, image.Height);

                ColorMap colorMap = new ColorMap();
                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                ColorMap[] remapTable = { colorMap };
                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
                float[][] colorMatrixElements =
                {
                    new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.7f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                };
                ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                int xpos = 0;
                int ypos = 0;

                xpos = ((image.Width - watermark.Width) - 10);
                ypos = image.Height - watermark.Height - 10;

                g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                watermark.Dispose();
                imageAttributes.Dispose();
                image.Dispose();
            }
            b.Save(path, format);
            return(true);
        }
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="Control"/> and its child controls and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_imageAttributes != null)
                {
                    _imageAttributes.Dispose();
                }
            }

            base.Dispose(disposing);
        }
Example #13
0
        private void PaintIcon(Graphics g, Bitmap bmp, Rectangle bounds, Color foreColor)
        {
            Rectangle destRect = new Rectangle(base.DataGridView.RightToLeftInternal ? ((bounds.Right - 3) - 12) : (bounds.Left + 3), bounds.Y + ((bounds.Height - 11) / 2), 12, 11);

            colorMap[0].NewColor = foreColor;
            colorMap[0].OldColor = Color.Black;
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetRemapTable(colorMap, ColorAdjustType.Bitmap);
            g.DrawImage(bmp, destRect, 0, 0, 12, 11, GraphicsUnit.Pixel, imageAttr);
            imageAttr.Dispose();
        }
Example #14
0
        private event PaintEventHandler _CustomPaint;                       // base.OnPaint interferes with our custom painting; mimic it to allow others in on the fun.


        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="ButtonExt"/> and optionally releases the managed
        /// resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to
        /// release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                DrawnImageAttributesDisabled?.Dispose();
                DrawnImageAttributesEnabled?.Dispose();
            }
            _CustomPaint = null;
            DrawnImageAttributesDisabled = null;
            DrawnImageAttributesEnabled  = null;
            base.Dispose(disposing);
        }
Example #15
0
        // takes an image and replaces all the pixels of oldColor with newColor, drawing the new image into the rectangle on
        // the supplied Graphics object.
        internal static void DrawImageReplaceColor(Graphics g, Image image, Rectangle dest, Color oldColor, Color newColor)
        {
            ImageAttributes attrs = new ImageAttributes();
            ColorMap        cm    = new ColorMap {
                OldColor = oldColor,
                NewColor = newColor
            };

            attrs.SetRemapTable(new ColorMap[] { cm }, ColorAdjustType.Bitmap);
            g.DrawImage(image, dest, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attrs, null, IntPtr.Zero);
            attrs.Dispose();
        }
Example #16
0
        /// <summary>
        /// On draw backbuffer
        /// </summary>
        protected override void OnDrawOnBackbuffer(ref System.Drawing.Graphics gfx)
        {
            // If the color matrix is empty, create it
            if (_cm == null)
            {
                _cm = new ColorMatrix(new float[][] {
                    new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.5f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                });
            }

            // Create new image attributes
            ImageAttributes ia = new ImageAttributes();

            ia.SetColorMatrix(_cm);

            // Get tile size
            Size tileSize = _background.TileSize;

            tileSize.Width  += 1;
            tileSize.Height += 1;

            // Iterate through instances
            foreach (GMareInstance inst in App.Room.Blocks)
            {
                // Get the object associated with this instance
                GMareObject obj = App.Room.Objects.Find(delegate(GMareObject o) { return(o.Resource.Id == inst.ObjectId); });

                // If no object was found, continue
                if (obj == null)
                {
                    continue;
                }

                // Get drawing point
                Point point = GMareBrush.TileIdToSourcePosition(inst.TileId, Image.Width, tileSize);
                point.X += 1;
                point.Y += 1;

                Bitmap image = obj.Image.ToBitmap();

                // Draw glyph
                gfx.DrawImage(image, new Rectangle(point.X, point.Y, obj.Image.Width, obj.Image.Height), 0, 0, obj.Image.Width, obj.Image.Height, GraphicsUnit.Pixel, ia);
                image.Dispose();
            }

            // Dispose.
            ia.Dispose();
        }
Example #17
0
 public void End()
 {
     if (!IsActive)
     {
         throw new InvalidOperationException("GuiRenderer is not active!");
     }
     IsActive = false;
     if (CurrentImageAttributes != null)
     {
         CurrentImageAttributes.Dispose();
         CurrentImageAttributes = null;
     }
 }
Example #18
0
        private void brightnessCorrectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float brightness = 45;

            if (brightnessCorrectionToolStripMenuItem.Checked)
            {
                Bitmap    TempBitmap       = new Bitmap(Image2.Image);
                float     FinalValue       = -brightness / 255.0f;
                Bitmap    NewBitmap        = new Bitmap(TempBitmap.Width, TempBitmap.Height);
                Graphics  NewGraphics      = Graphics.FromImage(NewBitmap);
                float[][] FloatColorMatrix =
                {
                    new float[] {          1,          0,          0, 0, 0 },
                    new float[] {          0,          1,          0, 0, 0 },
                    new float[] {          0,          0,          1, 0, 0 },
                    new float[] {          0,          0,          0, 1, 0 },
                    new float[] { FinalValue, FinalValue, FinalValue, 1, 1 }
                };

                ColorMatrix     NewColorMatrix = new ColorMatrix(FloatColorMatrix);
                ImageAttributes Attributes     = new ImageAttributes();
                Attributes.SetColorMatrix(NewColorMatrix);
                NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
                Attributes.Dispose();
                NewGraphics.Dispose();
                Image2.Image = NewBitmap;
            }
            else
            {
                Bitmap    TempBitmap       = new Bitmap(Image2.Image);
                float     FinalValue       = brightness / 255.0f;
                Bitmap    NewBitmap        = new Bitmap(TempBitmap.Width, TempBitmap.Height);
                Graphics  NewGraphics      = Graphics.FromImage(NewBitmap);
                float[][] FloatColorMatrix =
                {
                    new float[] {          1,          0,          0, 0, 0 },
                    new float[] {          0,          1,          0, 0, 0 },
                    new float[] {          0,          0,          1, 0, 0 },
                    new float[] {          0,          0,          0, 1, 0 },
                    new float[] { FinalValue, FinalValue, FinalValue, 1, 1 }
                };

                ColorMatrix     NewColorMatrix = new ColorMatrix(FloatColorMatrix);
                ImageAttributes Attributes     = new ImageAttributes();
                Attributes.SetColorMatrix(NewColorMatrix);
                NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
                Attributes.Dispose();
                NewGraphics.Dispose();
                Image2.Image = NewBitmap;
            }
        }
Example #19
0
    public static void AdjustBrightnessMatrix(Bitmap img, float value)

    {
        if (value == 0)     // No change, so just return

        {
            return;
        }



        float sb = (float)value / 255F;

        float[][] colorMatrixElements =

        {
            new float[] {  1,  0,  0, 0, 0 },

            new float[] {  0,  1,  0, 0, 0 },

            new float[] {  0,  0,  1, 0, 0 },

            new float[] {  0,  0,  0, 1, 0 },

            new float[] { sb, sb, sb, 1, 1 }
        };



        ColorMatrix cm = new ColorMatrix(colorMatrixElements);

        ImageAttributes imgattr = new ImageAttributes();

        Rectangle rc = new Rectangle(0, 0, img.Width, img.Height);

        Graphics g = Graphics.FromImage(img);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        imgattr.SetColorMatrix(cm);

        g.DrawImage(img, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr);



        //Clean everything up

        imgattr.Dispose();

        g.Dispose();
    }
Example #20
0
        //Aplica imaginii efectul de Polaroid;
        private void AplicaOldImage()
        {
            int width  = imageNew.Width;
            int height = imageNew.Height;

            image2.Dispose();
            image2 = new Bitmap(width, height);
            ImageAttributes imageAtr = new ImageAttributes();

            float[][] colorMatrixElements =
            {
                new float[] {   0.3f,   0.3f,   0.3f, 0, 0 },
                new float[] {  0.55f,  0.55f,  0.55f, 0, 0 },
                new float[] { 0.130f, 0.130f, 0.130f, 0, 0 },
                new float[] {      0,      0,      0, 1, 0 },
                new float[] {   0.2f,   0.2f,   0.2f, 0, 1 }
            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAtr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            Graphics G = Graphics.FromImage(image2);

            G.DrawImage(imageNew, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAtr);
            imageAtr.Dispose();
            G.Dispose();

            image3.Dispose();
            image3 = new Bitmap(EditorPoze.Properties.Resources.OldPaper, width, height);

            imageAtr = new ImageAttributes();

            float[][] colorMatrixElements2 =
            {
                new float[] {     1,     0,     0,    0, 0 },
                new float[] {     0,     1,     0,    0, 0 },
                new float[] {     0,     0,     1,    0, 0 },
                new float[] {     0,     0,     0, 0.4f, 0 },
                new float[] { -0.1f, -0.1f, -0.1f,    0, 1 }
            };

            ColorMatrix colorMatrix2 = new ColorMatrix(colorMatrixElements2);

            imageAtr.SetColorMatrix(colorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            G = Graphics.FromImage(image2);
            G.DrawImage(image3, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAtr);
            imageAtr.Dispose();
            G.Dispose();
        }
Example #21
0
        /// <summary>
        /// This gets called to instruct the element to draw itself in the appropriate spot of the graphics object
        /// </summary>
        /// <param name="g">The graphics object to draw to</param>
        /// <param name="printing">Boolean, true if this is being drawn to a print document</param>
        public override void Draw(Graphics g, bool printing)
        {
            if (_bitmap == null)
            {
                return;
            }

            //This color matrix is used to adjust how the image is drawn to the graphics object
            var bright = _brightness / 255.0F;
            var cont   = (_contrast + 255.0F) / 255.0F;

            float[][] colorArray =
            {
                new[]       { cont,        0,      0, 0, 0 },
                new[]       {      0, cont,        0, 0, 0 },
                new[]       {      0,      0, cont,   0, 0 },
                new float[] {      0,      0,      0, 1, 0 },
                new[]       { bright, bright, bright, 0, 1 }
            };
            var cm        = new ColorMatrix(colorArray);
            var imgAttrib = new ImageAttributes();

            imgAttrib.SetColorMatrix(cm);

            //Defines a parallelgram where the image is to be drawn
            var destPoints = new[]
            {
                LocationF,
                new PointF(LocationF.X + Size.Width, LocationF.Y),
                new PointF(LocationF.X, LocationF.Y + Size.Height)
            };

            var destSize = _bitmap.Size;

            if (_preserveAspectRatio)
            {
                if ((Size.Width / destSize.Width) < (Size.Height / destSize.Height))
                {
                    destPoints[2] = new PointF(LocationF.X, LocationF.Y + (Size.Width * destSize.Height / destSize.Width));
                }
                else
                {
                    destPoints[1] = new PointF(LocationF.X + (Size.Height * destSize.Width / destSize.Height), LocationF.Y);
                }
            }
            var srcRect = new Rectangle(0, 0, _bitmap.Width, _bitmap.Height);

            g.DrawImage(_bitmap, destPoints, srcRect, GraphicsUnit.Pixel, imgAttrib);
            imgAttrib.Dispose();
        }
        Bitmap TransformColor(Bitmap source, ColorMatrix matrix)
        {
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            Graphics g = Graphics.FromImage(source);

            g.DrawImage(source, new System.Drawing.Rectangle(0, 0, source.Width, source.Height), 0, 0,
                        source.Width, source.Height, GraphicsUnit.Pixel, imageAttributes);
            g.Dispose();
            imageAttributes.Dispose();
            return(source);
        }
Example #23
0
        //调整图片的gamma
        public static void AdjustGamma(ref Bitmap image, int percent)
        {
            ImageAttributes attr = new ImageAttributes();

            try
            {
                attr.SetGamma(GetGamma(percent));
                DrawImage(ref image, attr);
            }
            finally
            {
                attr.Dispose();
            }
        }
Example #24
0
        public void Contrast(PictureBox control)
        {
            float _contrastFactor = 0.04F * 28;

            control.Image = new Bitmap(source);
            Graphics        g            = Graphics.FromImage(control.Image);
            ImageAttributes IA           = new ImageAttributes();
            ColorMatrix     filterMatrix = new ColorMatrix(new float[][] { new float[] { _contrastFactor, 0f, 0f, 0f, 0f }, new float[] { 0f, _contrastFactor, 0f, 0f, 0f }, new float[] { 0f, 0f, _contrastFactor, 0f, 0f }, new float[] { 0f, 0f, 0f, 1f, 0f }, new float[] { 0.001f, 0.001f, 0.001f, 0f, 1f } });

            IA.SetColorMatrix(filterMatrix);
            g.DrawImage(control.Image, new Rectangle(0, 0, control.Image.Width, control.Image.Height), 0, 0, control.Image.Width, control.Image.Height, GraphicsUnit.Pixel, IA);
            g.Dispose();
            IA.Dispose();
        }
Example #25
0
        public Bitmap GammaController(Bitmap myBitMap, float value)
        {
            float           gamma     = 0.04f * value;
            Bitmap          newBitmap = new Bitmap(myBitMap.Width, myBitMap.Height);
            Graphics        g         = Graphics.FromImage(newBitmap);
            ImageAttributes ia        = new ImageAttributes();

            ia.SetGamma(gamma);
            g.DrawImage(myBitMap, new Rectangle(0, 0, myBitMap.Width, myBitMap.Height),
                        0, 0, myBitMap.Width, myBitMap.Height, GraphicsUnit.Pixel, ia);
            g.Dispose();
            ia.Dispose();
            return(newBitmap);
        }
        private void PaintBitmap(Graphics g, Bitmap b, Rectangle bounds)
        {
            int       x    = bounds.X + ((bounds.Width - b.Width) / 2);
            int       y    = bounds.Y + ((bounds.Height - b.Height) / 2);
            Rectangle rect = new Rectangle(x, y, b.Width, b.Height);

            g.FillRectangle(this.BackBrush, rect);
            ImageAttributes imageAttr = new ImageAttributes();

            this.colorMap[0].NewColor = this.ForeColor;
            imageAttr.SetRemapTable(this.colorMap, ColorAdjustType.Bitmap);
            g.DrawImage(b, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, imageAttr);
            imageAttr.Dispose();
        }
Example #27
0
 /// <summary>
 /// Draws the background image.
 /// </summary>
 /// <param name="g">The g.</param>
 /// <param name="backgroundImage">The background image.</param>
 /// <param name="backColor">Color of the back.</param>
 /// <param name="backgroundImageLayout">The background image layout.</param>
 /// <param name="bounds">The bounds.</param>
 /// <param name="clipRect">The clip rect.</param>
 /// <param name="scrollOffset">The scroll offset.</param>
 /// <param name="rightToLeft">The right to left.</param>
 /// <exception cref="System.ArgumentNullException">g</exception>
 public static void DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
 {
     if (g == null) {
         throw new ArgumentNullException(nameof(g));
     }
     if (backgroundImageLayout == ImageLayout.Tile) {
         using (TextureBrush textureBrush = new TextureBrush(backgroundImage, WrapMode.Tile)) {
             if (scrollOffset != Point.Empty) {
                 Matrix transform = textureBrush.Transform;
                 transform.Translate((float)scrollOffset.X, (float)scrollOffset.Y);
                 textureBrush.Transform = transform;
             }
             g.FillRectangle(textureBrush, clipRect);
             return;
         }
     }
     Rectangle rectangle = ControlPaintWrapper.CalculateBackgroundImageRectangle(bounds, backgroundImage, backgroundImageLayout);
     if (rightToLeft == RightToLeft.Yes && backgroundImageLayout == ImageLayout.None) {
         rectangle.X += clipRect.Width - rectangle.Width;
     }
     using (SolidBrush solidBrush = new SolidBrush(backColor)) {
         g.FillRectangle(solidBrush, clipRect);
     }
     if (!clipRect.Contains(rectangle)) {
         if (backgroundImageLayout == ImageLayout.Stretch || backgroundImageLayout == ImageLayout.Zoom) {
             rectangle.Intersect(clipRect);
             g.DrawImage(backgroundImage, rectangle);
             return;
         }
         if (backgroundImageLayout == ImageLayout.None) {
             rectangle.Offset(clipRect.Location);
             Rectangle destRect = rectangle;
             destRect.Intersect(clipRect);
             Rectangle rectangle2 = new Rectangle(Point.Empty, destRect.Size);
             g.DrawImage(backgroundImage, destRect, rectangle2.X, rectangle2.Y, rectangle2.Width, rectangle2.Height, GraphicsUnit.Pixel);
             return;
         }
         Rectangle destRect2 = rectangle;
         destRect2.Intersect(clipRect);
         Rectangle rectangle3 = new Rectangle(new Point(destRect2.X - rectangle.X, destRect2.Y - rectangle.Y), destRect2.Size);
         g.DrawImage(backgroundImage, destRect2, rectangle3.X, rectangle3.Y, rectangle3.Width, rectangle3.Height, GraphicsUnit.Pixel);
         return;
     }
     else {
         ImageAttributes imageAttributes = new ImageAttributes();
         imageAttributes.SetWrapMode(WrapMode.TileFlipXY);
         g.DrawImage(backgroundImage, rectangle, 0, 0, backgroundImage.Width, backgroundImage.Height, GraphicsUnit.Pixel, imageAttributes);
         imageAttributes.Dispose();
     }
 }
Example #28
0
 public override void Dispose()
 {
     if (_bitmap != null)
     {
         _bitmap.Dispose();
         _bitmap = null;
     }
     if (_ia != null)
     {
         _ia.Dispose();
         _ia = null;
     }
     base.Dispose();
 }
Example #29
0
        /// <summary>
        ///  加水印图片
        /// </summary>
        /// <param name="picture">imge 对象</param>
        /// <param name="WaterMarkPicPath">水印图片的地址</param>
        public static void addWatermarkImage(Image _Image, string WaterMarkPicPath)
        {
            try
            {
                Graphics picture   = Graphics.FromImage(_Image);
                Image    watermark = new Bitmap(WaterMarkPicPath);
                int      _width    = _Image.Width;
                int      _height   = _Image.Height;

                ImageAttributes imageAttributes = new ImageAttributes();
                ColorMap        colorMap        = new ColorMap();

                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                float[][] colorMatrixElements =
                {
                    new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.1f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                };

                ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                //imageAttributes.SetColorKey(System.Drawing.Color.Black, System.Drawing.Color.Black, ColorAdjustType.Bitmap);

                int xpos            = 0;
                int ypos            = 0;
                int WatermarkWidth  = 0;
                int WatermarkHeight = 0;
                //double bl = 1d;
                WatermarkWidth  = _width;
                WatermarkHeight = _height;
                xpos            = 0;
                ypos            = 0;

                picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                _Image = watermark;
                watermark.Dispose();
                imageAttributes.Dispose();
            }
            catch { }
        }
Example #30
0
 public override void Dispose()
 {
     if (_drawing != null)
     {
         _drawing.Dispose();
         _drawing = null;
     }
     if (_ia != null)
     {
         _ia.Dispose();
         _ia = null;
     }
     base.Dispose();
     _canvas = null;
 }
Example #31
0
	public static void ProcessImageColorMatrix (string sin, string sout, ColorMatrix colorMatrix)
	{
		Bitmap bmp_in = new Bitmap (sin);		
		Bitmap bmp_out = new Bitmap (bmp_in.Width, bmp_in.Height, bmp_in.PixelFormat);		
		
		Graphics gr = Graphics.FromImage (bmp_out);
		ImageAttributes imageAttr = new ImageAttributes ();
		
		imageAttr.SetColorMatrix (colorMatrix);

		gr.DrawImage (bmp_in, new Rectangle (0, 0, bmp_out.Width, bmp_out.Height), 
			0,0, bmp_out.Width, bmp_out.Height, GraphicsUnit.Pixel, imageAttr);		
		
		imageAttr.Dispose ();			
		bmp_out.Save (sout);
		bmp_in.Dispose ();
		bmp_out.Dispose ();
		Console.WriteLine ("Saving image file {0}", sout);
	}
Example #32
0
    /// <summary>

    ///   加水印图片

    /// </summary>

    /// <param name="picture">imge 对象</param>

    /// <param name="iTheImage">Image对象(以此图片为水印)</param>

    /// <param name="_watermarkPosition">水印位置</param>

    /// <param name="_width">被加水印图片的宽</param>

    /// <param name="_height">被加水印图片的高</param>

    private void addWatermarkImage(Graphics picture, Image iTheImage,

        string _watermarkPosition, int _width, int _height)
    {

        Image watermark = new Bitmap(iTheImage);

        var imageAttributes = new ImageAttributes();

        var colorMap = new ColorMap();

        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);

        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

        ColorMap[] remapTable = { colorMap };

        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

        float[][] colorMatrixElements = {
 
                                            new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
 
                                            new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
 
                                            new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
 
                                            new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
 
                                            new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
 
                                        };

        var colorMatrix = new ColorMatrix(colorMatrixElements);

        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        var xpos = 0;

        var ypos = 0;

        var WatermarkWidth = 0;

        var WatermarkHeight = 0;

        var bl = 1d;

        //计算水印图片的比率

        //取背景的1/4宽度来比较

        if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
        {

            bl = 1;

        }

        else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4))
        {

            bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);



        }

        else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
        {

            bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);

        }

        else
        {

            if ((_width * watermark.Height) > (_height * watermark.Width))
            {

                bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);



            }

            else
            {

                bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);



            }



        }

        WatermarkWidth = Convert.ToInt32(watermark.Width * bl);

        WatermarkHeight = Convert.ToInt32(watermark.Height * bl);

        switch (_watermarkPosition)
        {

            case "WM_TOP_LEFT":

                xpos = 10;

                ypos = 10;

                break;

            case "WM_TOP_RIGHT":

                xpos = _width - WatermarkWidth - 10;

                ypos = 10;

                break;

            case "WM_BOTTOM_RIGHT":

                xpos = _width - WatermarkWidth - 10;

                ypos = _height - WatermarkHeight - 10;

                break;

            case "WM_BOTTOM_LEFT":

                xpos = 10;

                ypos = _height - WatermarkHeight - 10;

                break;

        }

        picture.DrawImage(

            watermark,

            new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),

            0,

            0,

            watermark.Width,

            watermark.Height,

            GraphicsUnit.Pixel,

            imageAttributes);

        watermark.Dispose();

        imageAttributes.Dispose();

    }
Example #33
0
    /// <summary>
    /// 图片水印
    /// </summary>
    /// <param name="imgPath">服务器图片相对路径</param>
    /// <param name="filename">保存文件名</param>
    /// <param name="watermarkFilename">水印文件相对路径</param>
    /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
    /// <param name="quality">附加水印图片质量,0-100</param>
    /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
    public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
    {
        byte[] _ImageBytes = File.ReadAllBytes(Utils.GetMapPath(imgPath));
            Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
            filename = HttpContext.Current.Server.MapPath(filename);

            if (watermarkFilename.StartsWith("/") == false)
                watermarkFilename = "/" + watermarkFilename;
            watermarkFilename = Utils.GetMapPath(watermarkFilename);
            if (!File.Exists(watermarkFilename))
                return;
            Graphics g = Graphics.FromImage(img);
            //设置高质量插值法
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Image watermark = new Bitmap(watermarkFilename);

            if (watermark.Height >= img.Height || watermark.Width >= img.Width)
                return;

            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap colorMap = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            float transparency = 0.5F;
            if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
                transparency = (watermarkTransparency / 10.0F);

            float[][] colorMatrixElements = {
                                                new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                                new float[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f},
                                                new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int xpos = 0;
            int ypos = 0;

            switch (watermarkStatus)
            {
                case 1:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 2:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 3:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 4:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 5:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 6:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 7:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 8:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 9:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
            }

            g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1)
                    ici = codec;
            }
            EncoderParameters encoderParams = new EncoderParameters();
            long[] qualityParam = new long[1];
            if (quality < 0 || quality > 100)
                quality = 80;

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;

            if (ici != null)
                img.Save(filename, ici, encoderParams);
            else
                img.Save(filename);

            g.Dispose();
            img.Dispose();
            watermark.Dispose();
            imageAttributes.Dispose();
    }
Example #34
0
    /// <summary>
    /// 加图片水印
    /// </summary>
    /// <param name="imgPath">原图文件名物理路径</param>
    /// <param name="filename">生成文件名物理路径</param>
    /// <param name="watermarkFilename">水印文件名物理路径</param>
    /// <param name="positon">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 ... 9=右下</param>
    /// <param name="quality">附加图片质量,0--100之间,值大质量高</param>
    /// <param name="watermarkTransparency">水印的透明度 1--100 100为不透明</param>

    public static bool AddImageSignPic(string imgPath, string filename, string watermarkFilename, Enums.Position positon, int quality, int watermarkTransparency, int minWidth, int minHeight)
    {
        using (Bitmap img = new Bitmap(imgPath))
        {
            Graphics g ;
                //如果原图片是索引像素格式之列的,则需要转换
                if (IsPixelFormatIndexed(img.PixelFormat))
                {
                    using (Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb))
                    {
                        g = Graphics.FromImage(bmp);
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(img, 0, 0);
                    }

                }
                else
                {
                    g = Graphics.FromImage(img);
                    //设置高质量插值法
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //设置高质量,低速度呈现平滑程度
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                }
                int watermarkStatus = (int)positon;


                using (System.Drawing.Image watermark = new Bitmap(watermarkFilename))
                {

                    if (watermark.Height >= img.Height || watermark.Width >= img.Width)
                    {
                        return false;
                    }
                    if (img.Width < minWidth || img.Height < minHeight)
                    {
                        return false;
                    }


                    using (ImageAttributes imageAttributes = new ImageAttributes())
                    {
                        ColorMap colorMap = new ColorMap();

                        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                        ColorMap[] remapTable = { colorMap };

                        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                        float transparency = 0.5F;
                        if (watermarkTransparency >= 1 && watermarkTransparency <= 100)
                        {
                            transparency = (watermarkTransparency / 100.0F);
                        }

                        float[][] colorMatrixElements = {
                                                new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
                                                new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
                                                new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
                                                new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
                                                new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
                                            };

                        ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

                        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                        int xpos = 0;
                        int ypos = 0;

                        switch (watermarkStatus)
                        {
                            case 1:
                                xpos = (int)(img.Width * (float).01);
                                ypos = (int)(img.Height * (float).01);
                                break;
                            case 2:
                                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                                ypos = (int)(img.Height * (float).01);
                                break;
                            case 3:
                                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                                ypos = (int)(img.Height * (float).01);
                                break;
                            case 4:
                                xpos = (int)(img.Width * (float).01);
                                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                                break;
                            case 5:
                                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                                break;
                            case 6:
                                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                                break;
                            case 7:
                                xpos = (int)(img.Width * (float).01);
                                ypos = (int)((img.Height * (float).99) - watermark.Height);
                                break;
                            case 8:
                                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                                ypos = (int)((img.Height * (float).99) - watermark.Height);
                                break;
                            case 9:
                                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                                ypos = (int)((img.Height * (float).99) - watermark.Height);
                                break;
                        }

                        g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo ici = null;
                        foreach (ImageCodecInfo codec in codecs)
                        {
                            if (codec.MimeType.ToLower().IndexOf("jpeg") > -1 || codec.MimeType.ToLower().IndexOf("jpg") > -1)
                            {
                                ici = codec;
                            }
                        }
                        EncoderParameters encoderParams = new EncoderParameters();
                        long[] qualityParam = new long[1];
                        if (quality < 0 || quality > 100)
                        {
                            quality = 80;
                        }
                        qualityParam[0] = quality;

                        EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
                        encoderParams.Param[0] = encoderParam;

                        if (ici != null)
                        {
                            img.Save(filename, ici, encoderParams);
                        }
                        else
                        {
                            img.Save(filename);
                        }

                        g.Dispose();
                        img.Dispose();
                        watermark.Dispose();
                        imageAttributes.Dispose();
                    }
                
            }
        }
        return true;
    }
Example #35
0
    /// <summary>
    /// 添加图片水印
    /// </summary>
    /// <param name="oldFilePath">原始图片路径</param>
    /// <param name="newFilePath">将要添加水印图片路径</param>
    /// <param name="waterPosition">水印位置</param>
    /// <param name="waterImagePath">水印图片路径</param>
    /// <param name="transparency">透明度</param>
    /// <param name="quality">质量</param>
    public static void CreateWaterImage(string oldFilePath, string newFilePath, int waterPosition, string waterImagePath, int watermarkTransparency, int quality)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(oldFilePath);

        Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);

        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.White);

        g.DrawImage(image, 0, 0, image.Width, image.Height);

        //设置透明度
        System.Drawing.Image watermark = new Bitmap(waterImagePath);
        ImageAttributes imageAttributes = new ImageAttributes();
        ColorMap colorMap = new ColorMap();
        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
        ColorMap[] remapTable = { colorMap };
        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

        float transparency = 0.5F;
        if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
        {
            transparency = (watermarkTransparency / 10.0F);
        }

        float[][] colorMatrixElements = {
                                                    new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                                    new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                                    new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                                    new float[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f}, //注意:倒数第二处为0.0f为完全透明,1.0f为完全不透明
                                                    new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                                                };
        ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        int _width = image.Width;
        int _height = image.Height;
        int xpos = 0;
        int ypos = 0;
        int WatermarkWidth = 0;
        int WatermarkHeight = 0;
        double bl = 1d;
        //计算水印图片的比率
        //取背景的1/4宽度来比较
        if ((_width > watermark.Width * 2) && (_height > watermark.Height * 2))
        {
            bl = 1;
        }
        else if ((_width > watermark.Width * 2) && (_height < watermark.Height * 2))
        {
            bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);

        }
        else if ((_width < watermark.Width * 2) && (_height > watermark.Height * 2))
        {
            bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
        }
        else
        {
            if ((_width * watermark.Height) > (_height * watermark.Width))
            {
                bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);
            }
            else
            {
                bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
            }
        }
        WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
        WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
        switch (waterPosition)
        {
            case 3:
                xpos = _width - WatermarkWidth - 10;
                ypos = 10;
                break;
            case 2:
                xpos = 10;
                ypos = _height - WatermarkHeight - 10;
                break;
            case 5:
                xpos = _width / 2 - WatermarkWidth / 2;
                ypos = _height / 2 - WatermarkHeight / 2;
                break;
            case 1:
                xpos = 10;
                ypos = 10;
                break;
            case 4:
            default:
                xpos = _width - WatermarkWidth - 10;
                ypos = _height - WatermarkHeight - 10;
                break;
        }
        g.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
        try
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1)
                {
                    ici = codec;
                }
            }
            EncoderParameters encoderParams = new EncoderParameters();
            long[] qualityParam = new long[1];

            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;

            if (ici != null)
            {
                bmp.Save(newFilePath, ici, encoderParams);
            }
            else
            {
                bmp.Save(newFilePath);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            watermark.Dispose();
            imageAttributes.Dispose();
            image.Dispose();
            bmp.Dispose();
        }
    }
    public static void AdjustMatrixs(Bitmap img, float[] value)
    {

        if (value.Length == 0) 

            return;


        float sb = (float)value[0] / 255F;

        float[][] brMatrix =

                  { 

                        new float[] {1,  0,  0,  0, 0},

                        new float[] {0,  1,  0,  0, 0},

                        new float[] {0,  0,  1,  0, 0},

                        new float[] {0,  0,  0,  1, 0},

                        new float[] {sb, sb, sb, 1, 1}

                  };
        float co = 1F - (float)value[1] / 255F;

        float[][] coMatrix =

                  { 

                        new float[] {co,  0,  0,  0, 0},

                        new float[] {0,  co,  0,  0, 0},

                        new float[] {0,  0,  co,  0, 0},

                        new float[] {0,  0,  0, 1, 0},

                        new float[] {0,  0,  0,  0, 1}

                  };

        float[][] colorMatrixElements=Multiply(brMatrix,coMatrix);



        ColorMatrix cm = new ColorMatrix(colorMatrixElements);

        ImageAttributes imgattr = new ImageAttributes();

        Rectangle rc = new Rectangle(0, 0, img.Width, img.Height);

        Graphics g = Graphics.FromImage(img);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        imgattr.SetColorMatrix(cm);

        g.DrawImage(img, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr);



        //Clean everything up

        imgattr.Dispose();

        g.Dispose();

    }
    public static void AdjustContrastMatrix(Bitmap img, float value)
    {

        if (value == 0) // No change, so just return

            return;



        float co = 1F-(float)value / 255F;

        float[][] colorMatrixElements =

                  { 

                        new float[] {co,  0,  0,  0, 0},

                        new float[] {0,  co,  0,  0, 0},

                        new float[] {0,  0,  co,  0, 0},

                        new float[] {0,  0,  0, 1, 0},

                        new float[] {0,  0,  0,  0, 1}

                  };



        ColorMatrix cm = new ColorMatrix(colorMatrixElements);

        ImageAttributes imgattr = new ImageAttributes();

        Rectangle rc = new Rectangle(0, 0, img.Width, img.Height);

        Graphics g = Graphics.FromImage(img);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        imgattr.SetColorMatrix(cm);

        g.DrawImage(img, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr);

        imgattr.Dispose();

        g.Dispose();

    }