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; }
//uses a matrix to change the value of R G and B private void ChangeRGB() { redDivisible = RedTrackBar.Value; redDivisible /= 100; greenDivisible = GreenTrackBar.Value; greenDivisible /= 100; blueDivisible = BlueTrackBar.Value; blueDivisible /= 100; newImage = new Bitmap(previousImage.Width, previousImage.Height); Graphics g = Graphics.FromImage(newImage); ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { redDivisible, 0, 0, 0, 0 }, new float[] { 0, greenDivisible, 0, 0, 0 }, new float[] { 0, 0, blueDivisible, 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(previousImage, new Rectangle(0, 0, previousImage.Width, previousImage.Height), 0, 0, previousImage.Width, previousImage.Height, GraphicsUnit.Pixel, attributes); g.Dispose(); pictureBox1.Image = newImage; }
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; }
//uses a matrix to sepia-fy the image private void SepiaButton_Click(object sender, EventArgs e) { if ((pictureBox1.Image == null) && (pictureBox1.ImageLocation == null)) { MessageBox.Show("You have to select an image first!"); return; } newImage = new Bitmap(previousImage.Width, previousImage.Height); Graphics g = Graphics.FromImage(newImage); ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { 0.393f, 0.349f, 0.272f, 0, 0 }, new float[] { 0.769f, 0.686f, 0.534f, 0, 0 }, new float[] { 0.189f, 0.168f, 0.131f, 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(previousImage, new Rectangle(0, 0, previousImage.Width, previousImage.Height), 0, 0, previousImage.Width, previousImage.Height, GraphicsUnit.Pixel, attributes); g.Dispose(); pictureBox1.Image = newImage; previousImage = newImage; }
/// <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 VirtualPixel1D3(int pixels, [System.Runtime.CompilerServices.CallerMemberName] string name = "") : base(name) { if (pixels <= 0) throw new ArgumentOutOfRangeException("pixels"); this.pixelCount = pixels; this.devices = new List<PixelDevice>(); this.outputData.Subscribe(x => { Output(); }); Executor.Current.Blackout.Subscribe(_ => Output()); Executor.Current.Whiteout.Subscribe(_ => Output()); this.imageChanged = new Subject<Bitmap>(); this.outputBitmap = GetBitmap(); this.output = Graphics.FromImage(this.outputBitmap); this.outputRectangle = new Rectangle(0, 0, this.outputBitmap.Width, this.outputBitmap.Height); this.brightnessMatrix = 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[] {0.5f, 0.5f, 0.5f, 1, 1} }); }
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; }
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; }
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; } }
// 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(); }
/// <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; }
public ImageAttributes Trank(Bitmap btm, int Alphas) { Bitmap box = (Bitmap) btm.Clone(); Graphics.FromImage(box); float Alpha = ((float) Alphas) / 100f; float[][] CS_0_0000 = new float[5][]; float[] CS_0_0001 = new float[5]; CS_0_0001[0] = 1f; CS_0_0000[0] = CS_0_0001; float[] CS_0_0002 = new float[5]; CS_0_0002[1] = 1f; CS_0_0000[1] = CS_0_0002; float[] CS_0_0003 = new float[5]; CS_0_0003[2] = 1f; CS_0_0000[2] = CS_0_0003; float[] CS_0_0004 = new float[5]; CS_0_0004[3] = Alpha; CS_0_0000[3] = CS_0_0004; float[] CS_0_0005 = new float[5]; CS_0_0005[4] = 1f; CS_0_0000[4] = CS_0_0005; float[][] matrixltems = CS_0_0000; ColorMatrix colorMatrix = new ColorMatrix(matrixltems); ImageAttributes ImageAtt = new ImageAttributes(); ImageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); return ImageAtt; }
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(); //释放画刷 }
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; }
private Cursor GetDragCursor(Image image) { if (image == null) return Cursors.Default; image = ResizeImage(imageListView.ThumbnailSize.Width, image); var imageBounds = new Rectangle(new Point(0, 0), image.Size); var result = new Bitmap(imageBounds.Width, imageBounds.Height); var resultGraphics = Graphics.FromImage(result); float[][] matrixItems = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new[] { 0, 0, 0, 0.7f, 0 }, new float[] { 0, 0, 0, 0, 1 } }; var colorMatrix = new ColorMatrix(matrixItems); var imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); resultGraphics.DrawImage(image, imageBounds, imageBounds.X, imageBounds.Y, imageBounds.Width, imageBounds.Height, GraphicsUnit.Pixel, imageAttributes); var offset = new Point(imageBounds.Width / 2, imageBounds.Height / 2); return GridDragDropHelper.CreateCursor(result, offset); }
public static Image SetBrightness(Image image, float brightness) { float b = brightness; ColorMatrix cm = new ColorMatrix(new[] { new[] {b, 0, 0, 0, 0}, new[] {0, b, 0, 0, 0}, new[] {0, 0, b, 0, 0}, new[] {0, 0, 0, 1f, 0}, new[] {0, 0, 0, 0, 1f} }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(cm); Point[] points = { new Point(0, 0), new Point(image.Width - 1, 0), new Point(0, image.Height - 1), }; Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); Bitmap bm = new Bitmap(image.Width, image.Height); using (Graphics gr = Graphics.FromImage(bm)) { gr.DrawImage(image, points, rect, GraphicsUnit.Pixel, attributes); } // Return the result. return bm; }
public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix gMatrix, ColorMatrixFlag mode, ColorAdjustType type) { clrMatrix = newColorMatrix; this.gMatrix = gMatrix; clrMatrixFlag = mode; clrAdjustType = type; }
public static Bitmap AdjustBrightness(this Image Image, Color contrastMultiplier, Color brightnessBonus) { var TempBitmap = Image; var NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height); var NewGraphics = Graphics.FromImage(NewBitmap); float[][] FloatColorMatrix = { new[] { contrastMultiplier.R/255.0f, 0, 0, 0, 0 }, new[] { 0, contrastMultiplier.G/255.0f, 0, 0, 0 }, new[] { 0, 0, contrastMultiplier.B/255.0f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new[] { brightnessBonus.R/255.0f, brightnessBonus.G/255.0f, brightnessBonus.B/255.0f, 1, 1 } }; var NewColorMatrix = new ColorMatrix(FloatColorMatrix); var 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(); 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; }
/// <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; }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <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; }
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); } }
public static Bitmap AdjustBrightness(float bf, float cf, Bitmap bitmap) { ColorMatrix bm = new ColorMatrix(new float[][]{ new float[]{1f, 0f, 0f, 0f, 0f}, new float[]{0f, 1f, 0f, 0f, 0f}, new float[]{0f, 0f, 1f, 0f, 0f}, new float[]{0f, 0f, 0f, 1f, 0f}, new float[]{bf, bf, bf, 1f, 1f}}); //Create the contrast matrix. Note, the last row offsets the colors by .001 to avoid //arithmetic overflows that cause dramatically incorrect colors. //Thanks to Bob Powell for that tip: http://www.bobpowell.net/image_contrast.htm ColorMatrix cm = new ColorMatrix(new float[][]{ new float[]{cf, 0f, 0f, 0f, 0f}, new float[]{0f, cf, 0f, 0f, 0f}, new float[]{0f, 0f, cf, 0f, 0f}, new float[]{0f, 0f, 0f, 1f, 0f}, new float[]{0.001f, 0.001f, 0.001f, 0f, 1f}}); ColorMatrix adjust = null; if (bf != 0) adjust = bm; if (cf != 1) { if (adjust != null) adjust = ImageUtils.Multiply(adjust, cm); else adjust = cm; } if (adjust != null) return ImageHelper.ApplyColorMatrix(bitmap, adjust); else return bitmap; }
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 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> /// 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; } }
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; }
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 + "毫秒"; }
//************************************ // Color Filters //************************************ // Greyscale color matrix private void usingColorMatrixToolStripMenuItem_Click(object sender, EventArgs e) { // Create new bitmap based on size of opened picture Bitmap newBitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height); // Create graphics object image based on bitmap Graphics gObject = Graphics.FromImage(newBitmap); // Grayscale color matrix 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 image attributes ImageAttributes attributes = new ImageAttributes(); // Set color matrix attribute attributes.SetColorMatrix(colorMatrix); // Using grayscale matrix, draw orig image onto new image gObject.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), 0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height, GraphicsUnit.Pixel, attributes); gObject.Dispose(); pictureBox1.Image = newBitmap; }
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; }
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); }
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); } }
/// <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()); } }
//greyscale matrix private void GreyScale() { if ((pictureBox1.Image == null) && (pictureBox1.ImageLocation == null)) { MessageBox.Show("You have to select an image first!"); return; } if (greyIntitalised == false) { RedTrackBar.Value = 30; GreenTrackBar.Value = 59; BlueTrackBar.Value = 11; greyIntitalised = true; } greyPressed = true; this.Width = 1014; redDivisible = RedTrackBar.Value; redDivisible /= 100; greenDivisible = GreenTrackBar.Value; greenDivisible /= 100; blueDivisible = BlueTrackBar.Value; blueDivisible /= 100; greyBitmap = new Bitmap(previousImage.Width, previousImage.Height); Graphics g = Graphics.FromImage(greyBitmap); ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix( new float[][] { new float[] { redDivisible, redDivisible, redDivisible, 0, 0 }, new float[] { greenDivisible, greenDivisible, greenDivisible, 0, 0 }, new float[] { blueDivisible, blueDivisible, blueDivisible, 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(previousImage, new Rectangle(0, 0, previousImage.Width, previousImage.Height), 0, 0, previousImage.Width, previousImage.Height, GraphicsUnit.Pixel, attributes); g.Dispose(); pictureBox1.Image = greyBitmap; }
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 }
/// <summary> /// Create a disabled version of the image. /// </summary> /// <param name="image">The image to convert</param> /// <param name="background">The Color of the background behind the image. The background parameter is used to calculate the fill color of the disabled image so that it is always visible against the background.</param> /// <returns></returns> public static Image CreateDisabledImage(Image image, Color background) { if (image == null) { return(null); } Size imgSize = image.Size; if (_sDisabledImageAttr == null) { float[][] arrayJagged = new float[5][]; arrayJagged[0] = new float[5] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f }; arrayJagged[1] = new float[5] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f }; arrayJagged[2] = new float[5] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f }; float[] arraySingle = new float[5]; arraySingle[3] = 1f; arrayJagged[3] = arraySingle; arrayJagged[4] = new float[5] { 0.38f, 0.38f, 0.38f, 0f, 1f }; System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(arrayJagged); _sDisabledImageAttr = new System.Drawing.Imaging.ImageAttributes(); _sDisabledImageAttr.ClearColorKey(); _sDisabledImageAttr.SetColorMatrix(matrix); } Bitmap bitmap = new Bitmap(image.Width, image.Height); using (Graphics g = Graphics.FromImage(bitmap)) { g.DrawImage(image, new Rectangle(0, 0, imgSize.Width, imgSize.Height), 0, 0, imgSize.Width, imgSize.Height, GraphicsUnit.Pixel, _sDisabledImageAttr); } return(bitmap); }