public SetColorMatrix ( System.Drawing.Imaging.ColorMatrix newColorMatrix ) : void | ||
newColorMatrix | System.Drawing.Imaging.ColorMatrix | |
Résultat | void |
public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); if (applyRect.Width == 0 || applyRect.Height == 0) { // nothing to do return; } GraphicsState state = graphics.Save(); if (Invert) { graphics.SetClip(applyRect); graphics.ExcludeClip(rect); } ColorMatrix grayscaleMatrix = new ColorMatrix(new[] { new[] {.3f, .3f, .3f, 0, 0}, new[] {.59f, .59f, .59f, 0, 0}, new[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); using (ImageAttributes ia = new ImageAttributes()) { ia.SetColorMatrix(grayscaleMatrix); graphics.DrawImage(applyBitmap, applyRect, applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height, GraphicsUnit.Pixel, ia); } graphics.Restore(state); }
public static Bitmap GetGhostBitmap(string name) { ColorMatrix clrMatrix = new ColorMatrix(new float[][] { 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.5f, 0}, new float[] {0, 0, 0, 0, 1} }); ImageAttributes imgAttributes = new ImageAttributes(); imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Bitmap bitmap = GetBitmap(name); Bitmap ghostBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(ghostBitmap)) { g.FillRectangle(SystemBrushes.Window, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0,bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imgAttributes); } return ghostBitmap; }
public override void DrawEffectImage(Bitmap current, Bitmap next, EffectingPanel effecingPanel) { Bitmap doubleBufferingBitmap = null; // ダブルバッファリング用画面 Graphics bg = null; // ダブルバッファリング用画面描画用Graphics SolidBrush solidBrush = null; Rectangle rectangle; ColorMatrix colorMatrix = null; ImageAttributes imageAttributes = null; try { doubleBufferingBitmap = new Bitmap(current); bg = Graphics.FromImage(doubleBufferingBitmap); solidBrush = new SolidBrush(System.Drawing.Color.Black); rectangle = new Rectangle(0, 0, current.Width, current.Height); colorMatrix = new ColorMatrix(); imageAttributes = new ImageAttributes(); ResetInterval(); // フェードアウト for (float alpha = 0.9f; alpha >= 0.0f; alpha -= 0.05f) { bg.FillRectangle(solidBrush, rectangle); colorMatrix.Matrix33 = alpha; imageAttributes.SetColorMatrix(colorMatrix); bg.DrawImage(current, rectangle, 0, 0, doubleBufferingBitmap.Width, doubleBufferingBitmap.Height, GraphicsUnit.Pixel, imageAttributes); Thread.Sleep(10); effecingPanel.pictureBox.Image = doubleBufferingBitmap; effecingPanel.pictureBox.Refresh(); DoEventAtIntervals(); } for (float alpha = 0.0f; alpha <= 1.0f; alpha += 0.05f) { bg.FillRectangle(solidBrush, rectangle); colorMatrix.Matrix33 = alpha; imageAttributes.SetColorMatrix(colorMatrix); bg.DrawImage(next, rectangle, 0, 0, doubleBufferingBitmap.Width, doubleBufferingBitmap.Height, GraphicsUnit.Pixel, imageAttributes); Thread.Sleep(10); effecingPanel.pictureBox.Image = doubleBufferingBitmap; effecingPanel.pictureBox.Refresh(); DoEventAtIntervals(); } bg.Dispose(); } catch (SystemException ex) { Console.WriteLine(ex.Message); } }
internal static Image GetImage(Image originalImage, float opacity) { Image image = new Bitmap(originalImage); using (var g = Graphics.FromImage(image)) { using (var attributes = new ImageAttributes()) { var matrix = new ColorMatrix { Matrix40 = opacity, Matrix41 = opacity, Matrix42 = opacity }; attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); attributes.SetColorMatrix(matrix); g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } } return image; }
public static Bitmap Grayscale(Image b, float brightnessAdjust = 1) { Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat); // Create the ImageAttributes object and apply the ColorMatrix ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); attributes.SetColorMatrix(grayscaleMatrix); // Use a new Graphics object from the new image. using (Graphics g = Graphics.FromImage(bDest)) { // Draw the original image using the ImageAttributes created above. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, attributes); } if (brightnessAdjust != 1.0f) { return(Brightness(bDest, brightnessAdjust)); } return(bDest); }
public static Bitmap Inversion(Image b) { Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat); // Create the ImageAttributes object and apply the ColorMatrix ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix inversionMatrix = new ColorMatrix(new float[][] { 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[] { .99f, .99f, .99f, 0, 1 } }); attributes.SetColorMatrix(inversionMatrix); // Use a new Graphics object from the new image. using (Graphics g = Graphics.FromImage(bDest)) { // Draw the original image using the ImageAttributes created above. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, attributes); } return(bDest); }
public static Bitmap Brightness(Image b, float brightness) { Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat); float adjustedBrightness = brightness - 1.0f; // Create the ImageAttributes object and apply the ColorMatrix ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix brightnessMatrix = new ColorMatrix(new float[][] { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 0, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { brightness, brightness, brightness, 0, 1 } }); attributes.SetColorMatrix(brightnessMatrix); // Use a new Graphics object from the new image. using (Graphics g = Graphics.FromImage(bDest)) { // Draw the original image using the ImageAttributes created above. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, attributes); } return(bDest); }
public static Bitmap GetRowImage(GridView view, int rowHandle) { GridViewInfo gvi = view.GetViewInfo() as GridViewInfo; GridDataRowInfo ri = gvi.RowsInfo.OfType <GridDataRowInfo>().Where(r => r.RowHandle == rowHandle).FirstOrDefault(); Bitmap rowBmp = null; if (ri != null) { Bitmap gridBmp = new Bitmap(view.GridControl.Width, view.GridControl.Height); view.GridControl.DrawToBitmap(gridBmp, new Rectangle(0, 0, view.GridControl.Width, view.GridControl.Height)); float[][] matrixItems = new float[][] { 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.7F, 0 }, new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes(); attr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); rowBmp = new Bitmap(ri.DataBounds.Width, ri.DataBounds.Height); Graphics gr = Graphics.FromImage(rowBmp); Rectangle imageBounds = new Rectangle(Point.Empty, ri.DataBounds.Size); gr.DrawImage(gridBmp, imageBounds, ri.DataBounds.X, ri.DataBounds.Y, ri.DataBounds.Width, ri.DataBounds.Height, GraphicsUnit.Pixel, attr); } return(rowBmp); }
public void PrintScreenThreshold() { //Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); //System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(printscreen as Image); //graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size); Bitmap printscreen = new Bitmap(@"test\cap2.jpg"); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(printscreen)) { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold((float)0.50); // Change this threshold as needed (old .33) var rc = new Rectangle(0, 0, printscreen.Width, printscreen.Height); gr.DrawImage(printscreen, rc, 0, 0, printscreen.Width, printscreen.Height, GraphicsUnit.Pixel, ia); printscreen.Save("rewards.jpg", ImageFormat.Jpeg); } }
private void ColorFilter(float[][] values, PictureBox control) { this.Text = "Color - Processing..."; this.Cursor = System.Windows.Forms.Cursors.WaitCursor; if (control.Image == null) { control.Image = new Bitmap(source); } this.Cursor = System.Windows.Forms.Cursors.WaitCursor; Bitmap local_source = new Bitmap(control.Image); System.Drawing.Imaging.ColorMatrix filterMatrix = new System.Drawing.Imaging.ColorMatrix(values); System.Drawing.Imaging.ImageAttributes IA = new System.Drawing.Imaging.ImageAttributes(); IA.SetColorMatrix(filterMatrix); Bitmap filter = new Bitmap(local_source); using (Graphics G = Graphics.FromImage(filter)) { G.DrawImage(local_source, new Rectangle(0, 0, filter.Width, filter.Height), 0, 0, filter.Width, filter.Height, GraphicsUnit.Pixel, IA); } control.Image = filter; this.Text = "Color - Ready !"; this.Cursor = System.Windows.Forms.Cursors.Arrow; }
/// <summary> /// 指定された画像からネガティブイメージを作成する /// </summary> /// <param name="img">基の画像</param> /// <returns>作成されたネガティブイメージ</returns> public static Image CreateNegativeImage(Image img) { //ネガティブイメージの描画先となるImageオブジェクトを作成 Bitmap negaImg = new Bitmap(img.Width, img.Height); //negaImgのGraphicsオブジェクトを取得 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(negaImg); //ColorMatrixオブジェクトの作成 System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix(); //ColorMatrixの行列の値を変更して、色が反転されるようにする cm.Matrix00 = -1; cm.Matrix11 = -1; cm.Matrix22 = -1; cm.Matrix33 = 1; cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = cm.Matrix44 = 1; //ImageAttributesオブジェクトの作成 System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); //ColorMatrixを設定する ia.SetColorMatrix(cm); //ImageAttributesを使用して色が反転した画像を描画 g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); //リソースを解放する g.Dispose(); return(negaImg); }
public static Image InvertColors(Image image) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(image.Width, image.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); // create the negative color matrix ColorMatrix colorMatrix = new ColorMatrix(new[] { 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[] {1, 1, 1, 0, 1} }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); g.Dispose(); return newBitmap; }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// A Bitmap extension method that converts a bitmap to a gray scale image. /// </summary> /// <remarks> Aaevans, 2/16/2016. </remarks> /// <param name="bitmap"> The bitmap to act on. </param> /// <returns> A Bitmap. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static Bitmap AsGrayScaleImage(this Bitmap bitmap) { var newBitmap = new Bitmap(bitmap.Width, bitmap.Height); using (var g = Graphics.FromImage(newBitmap)) { var colorMatrix = new ColorMatrix( new[] { new[] { .3f, .3f, .3f, 0, 0 }, new[] { .59f, .59f, .59f, 0, 0 }, new[] { .11f, .11f, .11f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); var attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage( bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attributes); } return newBitmap; }
private static Bitmap Parlaklik(Bitmap bmp, int deger) { System.Drawing.Bitmap TempBitmap = bmp; float finalValue = (float)deger / 255.0f; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.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 }, }; System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix); System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return(NewBitmap); }
public static Image CreateGrayscaleImage(Image img) { var newImg = new Bitmap(img.Width, img.Height); var g = Graphics.FromImage(newImg); var cm = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { 0.3086f, 0.3086f, 0.3086f, 0, 0 }, new float[] { 0.6094f, 0.6094f, 0.6094f, 0, 0 }, new float[] { 0.0820f, 0.0820f, 0.0820f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(cm); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); g.Dispose(); return(newImg); }
private void pictureBox11_Click(object sender, EventArgs e) { undo.Push(pictureBox2.Image); imi1 = pictureBox2.Image; ImUndo = imi1; Bitmap originalImage = (Bitmap)pictureBox2.Image; Bitmap adjustedImage = (Bitmap)pictureBox2.Image; float brightness = 1.0f; // no change in brightness float contrast = Convert.ToSingle(numericUpDown1.Value); // twice the contrast float gamma = 1.0f; // no change in gamma float adjustedBrightness = brightness - 1.0f; // create matrix that will brighten and contrast the image float[][] ptsArray = { new float[] { contrast, 0, 0, 0, 0 }, // scale red new float[] { 0, contrast, 0, 0, 0 }, // scale green new float[] { 0, 0, contrast, 0, 0 }, // scale blue new float[] { 0, 0, 0, 1.0f, 0 }, // don't scale alpha new float[] { adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1 } }; System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(adjustedImage); g.DrawImage(originalImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height) , 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, imageAttributes); pictureBox2.Image = adjustedImage; sandi = adjustedImage; }
private Bitmap MakeGrayscaleWithColorMatrix(Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; }
//this function change the brightness of the image public static Bitmap AdjustBrightness(Bitmap Image, int Value) { System.Drawing.Bitmap TempBitmap = Image; float FinalValue = (float)Value / 255.0f; //calculate the scaling value System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.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 } }; //create the matrix for brightness calculation System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix); System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes(); Attributes.SetColorMatrix(NewColorMatrix); //redraw the image NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes); Attributes.Dispose(); NewGraphics.Dispose(); return(NewBitmap); }
/// <summary> /// To the negative. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public static Bitmap ToNegative(this Bitmap source) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(source.Width, source.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); // create the negative color matrix ColorMatrix colorMatrix = new ColorMatrix( new float[][] { 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[] {1, 1, 1, 0, 1} }); // create some image attributes ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; }
public static Bitmap Brightness(Image b, float brightness) { Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat); float adjustedBrightness = brightness - 1.0f; // Create the ImageAttributes object and apply the ColorMatrix ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix brightnessMatrix = new ColorMatrix(new float[][]{ new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 0, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {brightness, brightness, brightness, 0, 1} }); attributes.SetColorMatrix(brightnessMatrix); // Use a new Graphics object from the new image. using (Graphics g = Graphics.FromImage(bDest)) { // Draw the original image using the ImageAttributes created above. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, attributes); } return bDest; }
public Bitmap Offset(Bitmap bmpSource, Color clrScaleColor) { Bitmap bmpTemp = new Bitmap(bmpSource.Width, bmpSource.Height); //Create Temporary Bitmap To Work With ImageAttributes iaImageProps = new ImageAttributes(); //Contains information about how bitmap and metafile colors are manipulated during rendering. CMOffset = new ColorMatrix(new float[][] { 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[] { (float)clrScaleColor.R / 255, (float)clrScaleColor.G / 255, (float)clrScaleColor.B / 255, 0, 1 } }); iaImageProps.SetColorMatrix(combineColorMatrix()); //Apply Matrix Graphics grpGraphics = Graphics.FromImage(bmpTemp); //Create Graphics Object and Draw Bitmap Onto Graphics Object grpGraphics.DrawImage(bmpSource, new Rectangle(0, 0, bmpSource.Width, bmpSource.Height), 0, 0, bmpSource.Width, bmpSource.Height, GraphicsUnit.Pixel, iaImageProps); return bmpTemp; }
/// <summary> /// Converts image to a grayscale image /// </summary> public static Image ImageToGrayscale(Image image) { try { Bitmap bitmap = new Bitmap(image.Width, image.Height); Graphics graphics = Graphics.FromImage(bitmap); ColorMatrix matrix = new ColorMatrix(new float[][]{ new float[]{0.3f,0.3f,0.3f,0,0}, new float[]{0.59f,0.59f,0.59f,0,0}, new float[]{0.11f,0.11f,0.11f,0,0}, new float[]{0,0,0,1,0,0}, new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}}); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(matrix); graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); graphics.Dispose(); // Dispose temp graphics return (Image)bitmap; } catch (Exception ex) { ErrorManager.ShowError(ex); return image; } }
private void button2_Click(object sender, EventArgs e) { begin = DateTime.Now.Ticks; Bitmap currentBitmap = new Bitmap(pictureBox1.Image); Graphics g = Graphics.FromImage(currentBitmap); ImageAttributes ia = new ImageAttributes(); float[][] colorMatrix = { new float[] {0.299f, 0.299f, 0.299f, 0, 0}, new float[] {0.587f, 0.587f, 0.587f, 0, 0}, new float[] {0.114f, 0.114f, 0.114f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }; ColorMatrix cm = new ColorMatrix(colorMatrix); ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(currentBitmap, new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height), 0, 0, currentBitmap.Width, currentBitmap.Height, GraphicsUnit.Pixel, ia); pictureBox1.Image = currentBitmap; g.Dispose(); end = DateTime.Now.Ticks; label1.Text = (end - begin) / 1000 + "毫秒"; }
internal static Image InvertImage(Image myImage) { var invertedBmp = new Bitmap(myImage.Width, myImage.Height); //Setup color matrix var clrMatrix = new ColorMatrix(new[] { 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[] {1, 1, 1, 0, 1} }); using (var attr = new ImageAttributes()) { //Attach matrix to image attributes attr.SetColorMatrix(clrMatrix); using (var g = Graphics.FromImage(invertedBmp)) { g.DrawImage(myImage, new Rectangle(0, 0, myImage.Width, myImage.Height), 0, 0, myImage.Width, myImage.Height, GraphicsUnit.Pixel, attr); } } return invertedBmp; }
public static Bitmap ToGrayscale(this Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { .3f, .3f, .3f, 0, 0 }, new float[] { .59f, .59f, .59f, 0, 0 }, new float[] { .11f, .11f, .11f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); //create some image attributes System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return(newBitmap); }
/* * public static Image CreateDisabledImage(Image srcImage) * { * if (srcImage == null) return null; * return ToolStripRenderer.CreateDisabledImage(srcImage); * } */ public static Image CreateBlackWhite(Image srcImage) { if (srcImage == null) { return(null); } var newImage = (Image)srcImage.Clone(); using (var gr = Graphics.FromImage(newImage)) // SourceImage is a Bitmap object { var gray_matrix = new[] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.8f); // Change this threshold as needed var rc = new Rectangle(0, 0, newImage.Width, newImage.Height); gr.DrawImage(newImage, rc, 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia); } return(newImage); }
public static Graphics AlphaBlend(this Graphics gr, Bitmap b, Rectangle to, Rectangle from, float opacity) { #if WINCE byte bopacity = unchecked((byte)(255 * opacity)); using (Graphics gxSrc = Graphics.FromImage(b)) { IntPtr hdcDst = gr.GetHdc(); IntPtr hdcSrc = gxSrc.GetHdc(); BlendFunction blendFunction = new BlendFunction(); blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER; blendFunction.BlendFlags = (byte)BlendFlags.Zero; blendFunction.SourceConstantAlpha = bopacity; blendFunction.AlphaFormat = (byte)0; try{ PlatformAPI.AlphaBlend(hdcDst, to.X, to.Y, to.Width, to.Height, hdcSrc, from.X, from.Y, from.Width, from.Height, blendFunction); }catch(Exception e){} gr.ReleaseHdc(hdcDst); gxSrc.ReleaseHdc(hdcSrc); } #else var ia = new ImageAttributes(); float[][] ptsArray = { 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, opacity, 0}, new float[] {0, 0, 0, 0, 1}}; ia.SetColorMatrix(new ColorMatrix(ptsArray)); gr.DrawImage(b, to, from.X, from.Y, from.Width, from.Height, GraphicsUnit.Pixel, ia); #endif return gr; }
/// <summary> /// Executes this filter on the input image and returns the result /// </summary> /// <param name="inputImage">input image</param> /// <returns>transformed image</returns> public override Image ExecuteFilter(Image inputImage) { ///Reference from http://www.bobpowell.net/grayscale.htm Bitmap bm = new Bitmap(inputImage.Width, inputImage.Height); Graphics g = Graphics.FromImage(bm); ColorMatrix cm; if (_isBright) { cm = new ColorMatrix(new float[][]{ new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0}, new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}}); } else { //Gilles Khouzams colour corrected grayscale shear cm = new ColorMatrix(new float[][]{ new float[]{0.3f,0.3f,0.3f,0,0}, new float[]{0.59f,0.59f,0.59f,0,0}, new float[]{0.11f,0.11f,0.11f,0,0}, new float[]{0,0,0,1,0,0}, new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}}); } ImageAttributes ia = new ImageAttributes(); ia.SetColorMatrix(cm); g.DrawImage(inputImage, new Rectangle(0, 0, inputImage.Width, inputImage.Height), 0, 0, inputImage.Width, inputImage.Height, GraphicsUnit.Pixel, ia); g.Dispose(); return bm; }
internal static Image RGBToBGR(this Image bmp) { Image newBmp; if ((bmp.PixelFormat & PixelFormat.Indexed) != 0) { newBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb); } else { newBmp = bmp; } try { System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix(rgbtobgr); ia.SetColorMatrix(cm); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newBmp)) { g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel, ia); } } finally { if (newBmp != bmp) { bmp.Dispose(); } } return(newBmp); }
/// <summary> /// Converts a bitmap into grayscale mode /// </summary> public static Bitmap CreateGrayscaleBitmap(Bitmap fromBitmap) { Bitmap newBitmap = new Bitmap(fromBitmap.Width, fromBitmap.Height); using (Graphics gr = Graphics.FromImage(newBitmap)) { //create the grayscale ColorMatrix covert ramp ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); using (ImageAttributes attr = new ImageAttributes()) { attr.SetColorMatrix(colorMatrix); gr.DrawImage(fromBitmap, new Rectangle(0, 0, fromBitmap.Width, fromBitmap.Height), 0, 0, fromBitmap.Width, fromBitmap.Height, GraphicsUnit.Pixel, attr); } } return newBitmap; }
public static Bitmap GetGhostBitmap(string name) { float[][] newColorMatrix = new float[5][]; float[] numArray2 = new float[5]; numArray2[0] = 1f; newColorMatrix[0] = numArray2; numArray2 = new float[5]; numArray2[1] = 1f; newColorMatrix[1] = numArray2; numArray2 = new float[5]; numArray2[2] = 1f; newColorMatrix[2] = numArray2; numArray2 = new float[5]; numArray2[3] = 0.5f; newColorMatrix[3] = numArray2; numArray2 = new float[5]; numArray2[4] = 1f; newColorMatrix[4] = numArray2; ColorMatrix matrix = new ColorMatrix(newColorMatrix); ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Bitmap image = GetBitmap(name); Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(bitmap2)) { graphics.FillRectangle(SystemBrushes.Window, new Rectangle(0, 0, image.Width, image.Height)); graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); } return bitmap2; }
/// <summary> /// Processes the image. /// </summary> /// <param name="source">The current image to process</param> /// <param name="destination">The new Image to return</param> /// <returns> /// The processed <see cref="System.Drawing.Bitmap"/>. /// </returns> public override Bitmap TransformImage(Image source, Image destination) { using (Graphics graphics = Graphics.FromImage(destination)) { using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(this.Matrix); Rectangle rectangle = new Rectangle(0, 0, source.Width, source.Height); graphics.DrawImage(source, rectangle, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes); } } // Fade the contrast destination = Adjustments.Contrast(destination, -25); // Add a glow to the image. destination = Effects.Glow(destination, Color.FromArgb(70, 255, 153, 102)); // Add a vignette to finish the effect. destination = Effects.Vignette(destination, Color.FromArgb(220, 102, 34, 0)); return (Bitmap)destination; }
public static Bitmap MakeGrayscale(Bitmap original) { Bitmap newBitmap = new Bitmap(original.Width, original.Height); Graphics g = Graphics.FromImage(newBitmap); ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage(original, new System.Drawing.Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); g.Dispose(); return newBitmap; }
public static Image AdjustColors(float red, float green, float blue, Image original) { Bitmap image = new Bitmap(original); Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height); Graphics graphics = Graphics.FromImage(image); new SolidBrush(Color.FromArgb(50, 0, 0, 0)); float[][] numArray2 = new float[5][]; float[] numArray3 = new float[5]; numArray3[0] = red; numArray2[0] = numArray3; float[] numArray4 = new float[5]; numArray4[1] = green; numArray2[1] = numArray4; float[] numArray5 = new float[5]; numArray5[2] = blue; numArray2[2] = numArray5; float[] numArray6 = new float[5]; numArray6[3] = 1f; numArray2[3] = numArray6; float[] numArray7 = new float[5]; numArray7[4] = 1f; numArray2[4] = numArray7; float[][] newColorMatrix = numArray2; ColorMatrix matrix = new ColorMatrix(newColorMatrix); ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); graphics.Clear(Color.Transparent); graphics.DrawImage(original, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); graphics.Dispose(); return image; }
public override Bitmap Perform(Bitmap bitmap) { float contrastAdjusted = Contrast / 1000f + 1.0f; EnsurePixelFormat(ref bitmap); using (var g = Graphics.FromImage(bitmap)) { var attrs = new ImageAttributes(); attrs.SetColorMatrix(new ColorMatrix { Matrix00 = contrastAdjusted, Matrix11 = contrastAdjusted, Matrix22 = contrastAdjusted }); g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attrs); } return bitmap; }
public ucResizableImage(string image) { if (File.Exists(image) == false) return; Caption = ""; InitializeComponent(); imagePath = image; if (image.ToLower().EndsWith(".tga")) imageBMP = Paloma.TargaImage.LoadTargaImage(image); // http://www.codeproject.com/Articles/31702/NET-Targa-Image-Reader else imageBMP = (Bitmap)Bitmap.FromFile(image); SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); //create some image attributes grayscaleAttributes = new ImageAttributes(); //set the color matrix attribute grayscaleAttributes.SetColorMatrix(colorMatrix); }
public void BlackAndWhite() { using (Graphics gr = Graphics.FromImage(bmp)) // SourceImage is a Bitmap object { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; int width = bmp.Width; int height = bmp.Height; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold((float)0.8); // Change this threshold as needed var rc = new Rectangle(0, 0, bmp.Width, bmp.Height); gr.DrawImage(bmp, rc, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia); Bitmap b = new Bitmap(width, height, gr); OnImageFinished(b); } }
public static Image SetImageOpacity(Image image, float opacity) { try { //create a Bitmap the size of the image provided var bmp = new Bitmap(image.Width, image.Height); //create a graphics object from the image using (var gfx = Graphics.FromImage(bmp)) { //create a color matrix object var matrix = new ColorMatrix(); //set the opacity matrix.Matrix33 = opacity; //create image attributes var attributes = new ImageAttributes(); //set the color(opacity) of the image attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //now draw the image gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } return bmp; } catch { return null; } }
public static Bitmap ColorShift(Image b, Color cs) { Bitmap bDest = new Bitmap(b.Width, b.Height, b.PixelFormat); // Create the ImageAttributes object and apply the ColorMatrix ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix shiftMatrix = new ColorMatrix(new float[][]{ 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[] {cs.R/255f, cs.G/255f, cs.B/255f, 0, 1} }); attributes.SetColorMatrix(shiftMatrix); // Use a new Graphics object from the new image. using (Graphics g = Graphics.FromImage(bDest)) { // Draw the original image using the ImageAttributes created above. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, attributes); } return bDest; }
/// <summary> /// change brightness, contrast and/or gamma of a bitmap /// (see http://stackoverflow.com/questions/15408607/adjust-brightness-contrast-and-gamma-of-an-image) /// </summary> /// <param name="originalImage">image to process</param> /// <param name="brightness">new brightness (1.0 = no changes, 0.0 to 2.0)</param> /// <param name="contrast">new contrast (1.0 = no changes)</param> /// <param name="gamma">new gamma (1.0 = no changes)</param> /// <returns></returns> static internal Bitmap adjustBitmap(Bitmap originalImage, float brightness = 1.0f, float contrast = 1.0f, float gamma = 1.0f) { Bitmap adjustedImage; ImageAttributes imageAttributes; Graphics g; float adjustedBrightness; adjustedBrightness = brightness - 1.0f; // create matrix that will brighten and contrast the image float[][] ptsArray ={ new float[] {contrast, 0, 0, 0, 0}, // scale red new float[] {0, contrast, 0, 0, 0}, // scale green new float[] {0, 0, contrast, 0, 0}, // scale blue new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1}}; imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); adjustedImage = new Bitmap(originalImage.Width, originalImage.Height); g = Graphics.FromImage(adjustedImage); g.DrawImage(originalImage, new Rectangle(0,0,adjustedImage.Width,adjustedImage.Height), 0,0,originalImage.Width,originalImage.Height, GraphicsUnit.Pixel, imageAttributes); g.Dispose(); return adjustedImage; }
public override Bitmap Perform(Bitmap bitmap) { // convert +/-1000 input range to a logarithmic scaled multiplier float contrastAdjusted = (float) Math.Pow(2.718281f, Contrast / 500.0f); // see http://docs.rainmeter.net/tips/colormatrix-guide/ for offset & matrix calculation float offset = (1.0f - contrastAdjusted) / 2.0f; EnsurePixelFormat(ref bitmap); using (var g = Graphics.FromImage(bitmap)) { var attrs = new ImageAttributes(); attrs.SetColorMatrix(new ColorMatrix { Matrix00 = contrastAdjusted, Matrix11 = contrastAdjusted, Matrix22 = contrastAdjusted, Matrix33 = 1.0f, Matrix44 = 1.0f, Matrix40 = offset, Matrix41 = offset, Matrix42 = offset, }); g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, attrs); } return bitmap; }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics G = e.Graphics; //利用位图作为纹理创建纹理画刷 TextureBrush textureBrush1 = new TextureBrush(Properties.Resources.test); G.FillRectangle(textureBrush1, 40, 0, 40, 120); G.FillRectangle(textureBrush1, 0, 40, 120, 40); //利用位置的指定区域作为纹理创建纹理画刷 TextureBrush textureBrush2 = new TextureBrush(Properties.Resources.test, new Rectangle(10, 10, 28, 28)); G.FillRectangle(textureBrush2, 180, 0, 40, 120); G.FillRectangle(textureBrush2, 140, 40, 120, 40); TextureBrush textureBrush3 = new TextureBrush(Properties.Resources.test, new Rectangle(10, 10, 28, 28)); textureBrush3.WrapMode = WrapMode.TileFlipXY; //设置纹理图像的渐变方式 G.FillRectangle(textureBrush3, 30, 140, 60, 120); G.FillRectangle(textureBrush3, 0, 170, 120, 60); float[][] newColorMatrix = new float[][]{ //颜色变换矩形 new float[]{0.2f,0,0,0,0}, //红色分量 new float[]{0,0.6f,0,0,0}, //绿色分量 new float[]{0,0,0.2f,0,0}, //蓝色分量 new float[]{0,0,0,0.5f,0}, //透明度分量 new float[]{0,0,0,0,1}}; //始终为1 ColorMatrix colorMatrix = new ColorMatrix(newColorMatrix); ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); TextureBrush textureBrush4 = new TextureBrush(Properties.Resources.test, new Rectangle(0, 0, 48, 48), imageAttributes); textureBrush4.WrapMode = WrapMode.TileFlipXY; G.FillRectangle(textureBrush4, 170, 140, 60, 120); G.FillRectangle(textureBrush4, 140, 170, 120, 60); textureBrush1.Dispose(); //释放画刷 textureBrush2.Dispose(); //释放画刷 textureBrush3.Dispose(); //释放画刷 textureBrush4.Dispose(); //释放画刷 }
/// <summary> /// Processes the image. /// </summary> /// <param name="factory"> /// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing /// the image to process. /// </param> /// <param name="image">The current image to process</param> /// <param name="newImage">The new Image to return</param> /// <returns> /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// </returns> public Image TransformImage(ImageFactory factory, Image image, Image newImage) { using (Graphics graphics = Graphics.FromImage(newImage)) { using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(this.Matrix); Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height); graphics.DrawImage(image, rectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } } // Add a vignette to finish the effect. factory.Update(newImage); Vignette vignette = new Vignette(); newImage = (Bitmap)vignette.ProcessImage(factory); // Reassign the image. image.Dispose(); image = newImage; return image; }
/// <summary> /// Set brightness for the image /// </summary> /// <param name="image">input bitmap</param> /// <param name="value">value from -255 to 255</param> /// <returns></returns> public static Bitmap SetBrightness(Bitmap image, int value) { var tempBitmap = image; var finalValue = value / 255.0f; var newBitmap = new Bitmap(tempBitmap.Width, tempBitmap.Height); var 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[] { finalValue, finalValue, finalValue, 1, 1 } }; var newColorMatrix = new System.Drawing.Imaging.ColorMatrix(floatColorMatrix); var attributes = new System.Drawing.Imaging.ImageAttributes(); attributes.SetColorMatrix(newColorMatrix); newGraphics.DrawImage(tempBitmap, new System.Drawing.Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height), 0, 0, tempBitmap.Width, tempBitmap.Height, GraphicsUnit.Pixel, attributes); attributes.Dispose(); newGraphics.Dispose(); return(newBitmap); }
public static string ConvertImage(Bitmap image) { var matrix = CreateGrayscaleMatrix(); var attributes = new ImageAttributes(); attributes.SetColorMatrix(matrix); var asciiart = new StringBuilder(); using (var gphGrey = Graphics.FromImage(image)) { var bounds = new Rectangle(0, 0, image.Width, image.Height); gphGrey.DrawImage(image, bounds, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } int h = 0; // h := for each row int image, by image.Height and RowHeight { int w = 0; // w := for each column in image, by image.Width and ColumnWidth { var blockBrightness = GetBlockBrightness(image, h, w); var symbol = SymbolSelector.ChooseSymbol(blockBrightness); asciiart.Append(symbol); } asciiart.Append("\n"); } return asciiart.ToString(); }
// Obraz w skali szarości; internal void grayScale(MyBitmap myBitmap) { myBitmap.PreviousBitmap = (Bitmap)myBitmap.CurrentBitmap.Clone(); //Srednia skladowych // Wykorzystanie modelu YUV const float rMod = 0.333f; //rMod = 0.299f; const float gMod = 0.333f; //gMod = 0.587f; const float bMod = 0.333f; //bMod = 0.114f; Graphics g = Graphics.FromImage(myBitmap.CurrentBitmap); ColorMatrix colorMatrix = new ColorMatrix(new[] { new[] {rMod, rMod, rMod, 0, 1}, new[] {gMod, gMod, gMod, 0, 1}, new[] {bMod, bMod, bMod, 0, 1}, new[] {0.0f, 0.0f, 0.0f, 1, 1}, new[] {0.0f, 0.0f, 0.0f, 0, 1} }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); int x = myBitmap.BitmapInfo.SizeX; int y = myBitmap.BitmapInfo.SizeY; g.DrawImage(myBitmap.CurrentBitmap, new Rectangle(0, 0, x, y), 0, 0, x, y, GraphicsUnit.Pixel, attributes); //myBitmap.updateArrays(); g.Dispose(); }
private void FinalizePicture() { iaPicture.SetColorMatrix(cmPicture); gfxPicture = Graphics.FromImage(bmpPicture); rctPicture = new Rectangle(0, 0, imgPicture.Width, imgPicture.Height); gfxPicture.DrawImage(imgPicture, rctPicture, 0, 0, imgPicture.Width, imgPicture.Height, GraphicsUnit.Pixel, iaPicture); pictureBox1.Image = bmpPicture; }
internal static void RGBToBGR(this System.Drawing.Image bmp) { System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix(rgbtobgr); ia.SetColorMatrix(cm); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel, ia); } }
private void ResetPaintImage() { if (PaintImage != null) { PaintImage.Dispose(); PaintImage = null; } if (Image != null && Image.Width > 0 && Image.Height > 0 && DrawRect.Width > 0 && DrawRect.Height > 0) { PaintImage = new Bitmap((int)DrawRect.Width, (int)DrawRect.Height); using (Graphics g = Graphics.FromImage(PaintImage)) { System.Drawing.Imaging.ImageAttributes ima = new System.Drawing.Imaging.ImageAttributes(); ColorMatrix cm = new ColorMatrix(); cm.Matrix33 = Opacity; ima.SetColorMatrix(cm); Point pt = Point.Empty; switch (FillMode) { case ImageFillMode.Center: pt = new Point((int)(DrawRect.Width - Image.Width) / 2, (int)(DrawRect.Height - Image.Height) / 2); g.DrawImage(Image, new Rectangle(pt, Image.Size), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, ima); break; case ImageFillMode.Strength: g.DrawImage(Image, Rectangle.Round(DrawRect), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, ima); break; case ImageFillMode.Title: ima.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile); TextureBrush brush = new TextureBrush(Image, new Rectangle(0, 0, Image.Width, Image.Height), ima); g.FillRectangle(brush, DrawRect); break; case ImageFillMode.Zoom: float scale = 1; if (Image.Width > 0 && Image.Height > 0 && DrawRect.Width > 0 && DrawRect.Height > 0) { float f1 = DrawRect.Width / Image.Width; float f2 = DrawRect.Height / Image.Height; scale = f1 > f2 ? f2 : f1; float zWidth = scale * Image.Width; float zHeight = scale * Image.Height; //RectangleF zoomRect = new RectangleF( } break; } } } else { PaintImage = null; } }
public static Bitmap ChangeOpacity(Image img, float opacityvalue) { Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image Graphics graphics = Graphics.FromImage(bmp); System.Drawing.Imaging.ColorMatrix colormatrix = new System.Drawing.Imaging.ColorMatrix(); colormatrix.Matrix33 = opacityvalue; System.Drawing.Imaging.ImageAttributes imgAttribute = new System.Drawing.Imaging.ImageAttributes(); imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute); graphics.Dispose(); // Releasing all resource used by graphics return(bmp); }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="path">原图片绝对地址</param> /// <param name="suiyi">水印文件</param> /// <param name="pos">水印位置</param> private static byte[] addWaterMark(Image image, string suiyi, WaterPositionMode pos) { try { Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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, 1.0f, 0.0f },//设置透明度 new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; Point position = SetMarkPoint(pos, watermark, image); xpos = position.X; // ((image.Width - watermark.Width) - 50);//水印位置 ypos = position.Y; //image.Height - watermark.Height - 50;//水印位置 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); MemoryStream ms = new MemoryStream(); b.Save(ms, ImageFormat.Bmp); byte[] byteImage = ms.ToArray(); b.Dispose(); image.Dispose(); g.Dispose(); return(byteImage); } catch (Exception ex) { MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Bmp); return(ms.ToArray()); } }
static void RenderTo(this ImageDrawing drawing, d.Graphics graphics, double opacity) { var image = drawing.ImageSource.ToGdiPlus(); if (image != null) { var ia = new di.ImageAttributes(); ia.SetColorMatrix(new di.ColorMatrix { Matrix33 = (float)opacity }); var r = drawing.Rect; graphics.DrawImage(image, new[] { r.TopLeft.ToGdiPlus(), r.TopRight.ToGdiPlus(), r.BottomLeft.ToGdiPlus() }, new d.RectangleF(0, 0, image.Width, image.Height), d.GraphicsUnit.Pixel, ia); } }
/// <summary> /// 添加图片水印 /// </summary> /// <param name="path">原图片绝对地址</param> /// <param name="suiyi">水印文件</param> public void addWaterMark(string oldfile, string newfile, string suiyi) { System.Drawing.Image image = System.Drawing.Image.FromFile(oldfile); Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(b); g.Clear(Color.White); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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, 1.0f, 0.0f },//设置透明度0.3f new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; xpos = (image.Width - watermark.Width) / 2; //水印位置 ypos = (image.Height - watermark.Height) / 2; //水印位置 g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); b.Save(newfile); b.Dispose(); image.Dispose(); if (File.Exists(oldfile))//删除原始文件 { File.Delete(oldfile); } }
/// <summary> /// change constract /// </summary> /// <param name="img"></param> /// <param name="contrast"></param> /// <returns></returns> public static Image chContrast(Image _img, float contrast) { if (contrast <= 0) { return(_img); } Bitmap newImg = new Bitmap(_img.Width, _img.Height); Graphics g = Graphics.FromImage(newImg); try { //ColorMatrix float _s = (100f + contrast) / 100f; _s *= _s; float append = 0.5f * (1f - _s); System.Drawing.Imaging.ColorMatrix _cm = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { _s, 0, 0, 0, 0 }, new float[] { 0, _s, 0, 0, 0 }, new float[] { 0, 0, _s, 0, 0 }, new float[] { 0, 0, 0, _s, 0 }, new float[] { append, append, append, 0, 1 } }); //ImageAttributes System.Drawing.Imaging.ImageAttributes _ia = new System.Drawing.Imaging.ImageAttributes(); _ia.SetColorMatrix(_cm); //ImageAttributes g.DrawImage(_img, new Rectangle(0, 0, _img.Width, _img.Height), 0, 0, _img.Width, _img.Height, GraphicsUnit.Pixel, _ia); } catch (Exception e) { Console.WriteLine(e.Message); return(_img); } finally { g.Dispose(); } return(newImg); }
static private Bitmap watermark(string path) { string suiyi = ConfigurationManager.AppSettings["Watermark"]; if (suiyi == null) { return(null); } System.Drawing.Image image = System.Drawing.Image.FromFile(path); Bitmap b = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); g.DrawImage(image, 0, 0, image.Width, image.Height); System.Drawing.Image watermark = new Bitmap(suiyi); System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); System.Drawing.Imaging.ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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.2f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; xpos = ((image.Width - watermark.Width) - 0); ypos = image.Height - watermark.Height - 0; g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); watermark.Dispose(); imageAttributes.Dispose(); return(b); }
public void ToGrayScale(Bitmap SourceImage) { using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.5F); // Change this threshold as needed var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height); gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia); } }
public object Create(Image image, float opacity) { var sdimage = new sd.Bitmap(image.ToSD()); var att = new sdi.ImageAttributes(); att.SetWrapMode(sd2.WrapMode.Tile); if (opacity < 1.0f) { var colorMatrix = new sdi.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, opacity, 0.0f }, new float [] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }); att.SetColorMatrix(colorMatrix); } return(new sd.TextureBrush(sdimage, new sd.RectangleF(0, 0, sdimage.Width, sdimage.Height), att)); }
public static ImageAttributes GetTransparentBitmapAttributes(int PercentTransparent) { if (BitmapAttributesCache.ContainsKey(PercentTransparent)) { return(BitmapAttributesCache[PercentTransparent]); } System.Drawing.Imaging.ImageAttributes _IAttr = new System.Drawing.Imaging.ImageAttributes(); System.Drawing.Imaging.ColorMatrix ClrMatrix = new System.Drawing.Imaging.ColorMatrix(); #region Setup Color Matrix For .25 Transparent ClrMatrix.Matrix00 = 1.0f; ClrMatrix.Matrix01 = 0.0f; ClrMatrix.Matrix02 = 0.0f; ClrMatrix.Matrix03 = 0.0f; ClrMatrix.Matrix04 = 0.0f; ClrMatrix.Matrix10 = 0.0f; ClrMatrix.Matrix11 = 1.0f; ClrMatrix.Matrix12 = 0.0f; ClrMatrix.Matrix13 = 0.0f; ClrMatrix.Matrix14 = 0.0f; ClrMatrix.Matrix20 = 0.0f; ClrMatrix.Matrix21 = 0.0f; ClrMatrix.Matrix22 = 1.0f; ClrMatrix.Matrix23 = 0.0f; ClrMatrix.Matrix24 = 0.0f; ClrMatrix.Matrix30 = 0.0f; ClrMatrix.Matrix31 = 0.0f; ClrMatrix.Matrix32 = 0.0f; ClrMatrix.Matrix33 = (float)((float)PercentTransparent / (float)100); ClrMatrix.Matrix34 = 0.0f; ClrMatrix.Matrix40 = 0.0f; ClrMatrix.Matrix41 = 0.0f; ClrMatrix.Matrix42 = 0.0f; ClrMatrix.Matrix43 = 0.0f; ClrMatrix.Matrix44 = 1.0f; _IAttr.SetColorMatrix(ClrMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); BitmapAttributesCache.Add(PercentTransparent, _IAttr); return(BitmapAttributesCache[PercentTransparent]); #endregion }
public void convertToBlackAndWhite(Bitmap input) { using (Graphics gr = Graphics.FromImage(input)) // SourceImage is a Bitmap object { var gray_matrix = new float[][] { new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var ia = new System.Drawing.Imaging.ImageAttributes(); ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix)); ia.SetThreshold(0.8f); // Change this threshold as needed var rc = new Rectangle(0, 0, input.Width, input.Height); gr.DrawImage(input, rc, 0, 0, input.Width, input.Height, GraphicsUnit.Pixel, ia); } }
private static System.Drawing.Imaging.ImageAttributes GetImageAlphaAttributes(float alpha) { float[][] ptsArray = { 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, alpha, 0 }, new float[] { 0, 0, 0, 0, 1 } }; System.Drawing.Imaging.ColorMatrix clrMatrix = new System.Drawing.Imaging.ColorMatrix(ptsArray); System.Drawing.Imaging.ImageAttributes imgAttrs = new System.Drawing.Imaging.ImageAttributes(); imgAttrs.SetColorMatrix(clrMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); return(imgAttrs); }