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; }
/// <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 static Bitmap AdjustImage(Bitmap original, float brightness = 1.0f, float contrast = 1.0f, float gamma = 1.0f) { var adjustedImage = new Bitmap(original.Width, original.Height); 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}}; var ia = new ImageAttributes(); ia.ClearColorMatrix(); ia.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); ia.SetGamma(gamma, ColorAdjustType.Bitmap); using (var g = Graphics.FromImage(adjustedImage)) { g.DrawImage(original, new Rectangle(Point.Empty, adjustedImage.Size), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia); } return adjustedImage; }
/// <summary> /// Adjusts the red, green and blue parts of the image /// /// Examples: /// Original image should be dark/black (main color RGB(51,51,51) and less) /// /// blue (metro): 0,1,3,0.55 /// light grey: 1,1,1,0.35 /// white: 1,1,1,0.01 /// green: 0,2,0,0.75 /// red: 2,0,0,0.75 /// </summary> /// <param name="img">Image</param> /// <param name="red">red part adjustment (>= 0.00, 1.0 = no changes)</param> /// <param name="green">red part adjustment (>= 0.00, 1.0 = no changes)</param> /// <param name="blue">blue part adjustment (>= 0.00, 1.0 = no changes)</param> /// <param name="gamma">Gamma adjustment (> 0.00, 1.0 = no changes)</param> /// <returns>the modified Image</returns> public static Image AdjustRGBGamma(this Image img, float red, float green, float blue, float gamma) { Bitmap bmp = new Bitmap(img.Width, img.Height); ImageAttributes imgAttributes = new ImageAttributes(); ColorMatrix matrix = new ColorMatrix( new float[][] { new float[] { red, 0, 0, 0, 0 }, new float[] { 0, green, 0, 0, 0 }, new float[] { 0, 0, blue, 0, 0 }, new float[] { 0, 0, 0, 1.0f, 0 }, new float[] { 0, 0, 0, 0, 1 }, }); imgAttributes.ClearColorMatrix(); imgAttributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imgAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes); } return (Image)bmp; }
//***********************************鼠标落**********************************************************************************************************// private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (!cutoff) { if (e.Button == MouseButtons.Right) { if (textBox1.Text.Length != 0 && pictureBox1.Image != null) { Bitmap bt = new Bitmap(pictureBox1.Image); string myfont = textBox1.Text.Trim(); Graphics gg = Graphics.FromImage(bt); FontF = fontDialog1.Font.FontFamily; gg.DrawString(textBox1.Text.Trim(), new Font(FontF, fontSize, fontStyle), new SolidBrush(fontColor), new PointF(e.X, e.Y)); pictureBox1.Image = bt; } } else { if (moveflag == true) { start = new Point(e.X, e.Y); end = start; } int x, y; x = e.X * bitmap.Width / pictureBox1.Width; y = e.Y * bitmap.Height / pictureBox1.Height; Color c = bitmap.GetPixel(x, y); x_x = x; y_y = y; mousedown = true; if (flag == 0) { Rectangle dest2 = new Rectangle(1, 1, pictureBox2.Width, pictureBox2.Height); System.Drawing.Imaging.ImageAttributes imageattr = new System.Drawing.Imaging.ImageAttributes(); imageattr.SetGamma(1.0F); g2.DrawImage(pictureBox1.Image, dest2, x, y, pictureBox2.Width, pictureBox2.Height, GraphicsUnit.Pixel); pictureBox2.BackColor = Color.Transparent; Invalidate(true); } } } else { this.Cursor = Cursors.Cross; this.p1 = new Point(e.X, e.Y); x1 = e.X; y1 = e.Y; if (this.pictureBox1.Image != null) { HeadImageBool = true; } else { HeadImageBool = false; } } }
public override void Draw(Graphics g, int x, int y, Size sizTile, TemplateDoc tmpd, LayerType layer, bool fSelected) { if (layer == LayerType.Scenery) { Bitmap[] abm = m_gimg.GetBitmapSides(sizTile); Bitmap bm = abm[0]; if (fSelected) { Rectangle rcDst = new Rectangle(x, y, bm.Width, bm.Height); ImageAttributes attr = new ImageAttributes(); attr.SetGamma(0.5f); g.DrawImage(bm, rcDst, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attr); } else { g.DrawImage(bm, x, y); } } }
/// <param name="img"></param> /// <param name="value">1 = No change (Min 0.1, Max 5.0)</param> public static Image ChangeGamma(Image img, float value) { value = value.Between(0.1f, 5.0f); Bitmap bmp = img.CreateEmptyBitmap(); using (Graphics g = Graphics.FromImage(bmp)) using (ImageAttributes ia = new ImageAttributes()) { ia.ClearColorMatrix(); ia.SetGamma(value, ColorAdjustType.Bitmap); g.SetHighQuality(); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); } return bmp; }
public static Image ChangeGamma(Image img, float gamma) { Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); gamma = gamma / 100 + 1; gamma = gamma.Between(0.1f, 10.0f); using (Graphics g = Graphics.FromImage(bmp)) { using (ImageAttributes imgattr = new ImageAttributes()) { imgattr.SetGamma(gamma, ColorAdjustType.Bitmap); g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr); } } return bmp; }
/// <summary> /// Метод изменения яркости и контрастности изображения /// </summary> /// <param name="image">Изображение</param> /// <param name="brightness">Яркость</param> /// <param name="gamma">Гамма</param> /// <param name="cRed">Контраст красного</param> /// <param name="cGreen">Контраст зеленого</param> /// <param name="cBlue">Контраст синего</param> /// <param name="cAlpha">Прозрачность</param> /// <returns>Новое изображение</returns> public static Bitmap AdjustImage(Image image, float brightness = 1.0f, float gamma = 1.0f, float cRed = 1.0f, float cGreen = 1.0f, float cBlue = 1.0f, float cAlpha = 1.0f) { brightness -= 1.0f; // Значение яркости указывается в виде дельты, то есть 0 - нет изменений, 0.5 - увеличить на 50%, -0.5 - уменьшить на 50% и т.д. float[][] matrix = { new float[] { cRed, 0, 0, 0, 0 }, // Контраст красного new float[] { 0, cGreen, 0, 0, 0 }, // Контраст зеленого new float[] { 0, 0, cBlue, 0, 0 }, // Контраст синего new float[] { 0, 0, 0, cAlpha, 0 }, // Контраст альфы new float[] { brightness, brightness, brightness, 0, 1 } // Яркость }; ImageAttributes atts = new ImageAttributes(); atts.SetColorMatrix(new ColorMatrix(matrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); atts.SetGamma(gamma, ColorAdjustType.Bitmap); Bitmap newImage = new Bitmap(image.Width, image.Height); using (var gfx = Graphics.FromImage(newImage)) gfx.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, atts); return newImage; }
private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { // 리셋 case Keys.Space: Attr = new ImageAttributes(); Src = Image.FromFile("오솔길.jpg"); Threshold = 0.5f; Gamma = 1.0f; Bright = 0.0f; break; // 시계 방향 회전 case Keys.Right: Src.RotateFlip(RotateFlipType.Rotate90FlipNone); break; // 반시계 방향 회전 case Keys.Left: Src.RotateFlip(RotateFlipType.Rotate270FlipNone); break; // 수평 뒤집기 case Keys.Up: Src.RotateFlip(RotateFlipType.RotateNoneFlipX); break; // 수직 뒤집기 case Keys.Down: Src.RotateFlip(RotateFlipType.RotateNoneFlipY); break; // 스레시 홀드 증감 case Keys.Q: if (Threshold < 1) Threshold += 0.1f; Attr.SetThreshold(Threshold); break; case Keys.W: if (Threshold > 0) Threshold -= 0.1f; Attr.SetThreshold(Threshold); break; // 감마 증감 case Keys.E: if (Gamma < 5.0) Gamma += 0.1f; Attr.SetGamma(Gamma); break; case Keys.R: if (Gamma > 0.1) Gamma -= 0.1f; Attr.SetGamma(Gamma); break; // 밝게 case Keys.D1: if (Bright < 1.0f) Bright += 0.1f; float[][] M1 = { 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[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat1 = new ColorMatrix(M1); Attr.SetColorMatrix(Mat1); break; // 어둡게 case Keys.D2: if (Bright > -1.0f) Bright -= 0.1f; float[][] M2 = { 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[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat2 = new ColorMatrix(M2); Attr.SetColorMatrix(Mat2); break; // 반전 case Keys.D3: float[][] M3 = { 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[] {1.0f, 1.0f, 1.0f, 0.0f, 1.0f}, }; ColorMatrix Mat3 = new ColorMatrix(M3); Attr.SetColorMatrix(Mat3); break; // 그레이 스케일 case Keys.D4: float[][] M4 = { new float[] {0.299f, 0.299f, 0.299f, 0.0f, 0.0f}, new float[] {0.587f, 0.587f, 0.587f, 0.0f, 0.0f}, new float[] {0.114f, 0.114f, 0.114f, 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}, }; ColorMatrix Mat4 = new ColorMatrix(M4); Attr.SetColorMatrix(Mat4); break; case Keys.D5: ColorMap[] Map = new ColorMap[1]; Map[0] = new ColorMap(); Map[0].OldColor = Color.White; Map[0].NewColor = Color.Blue; Attr.SetRemapTable(Map); break; } Invalidate(); }
protected override void OnPaint(PaintEventArgs e) { if (_imageSlot != null) { var attr = new System.Drawing.Imaging.ImageAttributes(); int top = Top; int bottom = Top + Height; int left = Left; int right = Left + Width; attr.SetGamma(gamma); int squarish = Math.Max(_imageSlot.Width, _imageSlot.Height); squarish = (int)(squarish * 1.25); if (renderBack) e.Graphics.DrawImage(_imageBack, new Rectangle((Width / 2) - (squarish / 2), (Height / 2) - (squarish / 2), squarish, squarish), new Rectangle(0, 0, _imageBack.Width, _imageBack.Height), GraphicsUnit.Pixel); //e.Graphics.DrawImage(_imageBack, (Width / 2) - (_imageBack.Width / 2), (Height / 2) - (_imageBack.Height / 2)); //e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2)); //Point[] dest = new Point[]{ new Point(top, left), new Point(top, right), new Point(bottom, right)}; e.Graphics.DrawImage(_imageSlot, new Rectangle((Width / 2) - (_imageSlot.Width / 2), (Height / 2) - (_imageSlot.Height / 2), _imageSlot.Width, _imageSlot.Height), 0, 0, _imageSlot.Width, _imageSlot.Height, GraphicsUnit.Pixel, attr); int smallish = (int)(squarish * 0.5); if (coolness != 0) { float[] colour = new float[] { 0, 0.6f, 0, 0, 1 }; if (coolness == 2) colour = new float[] { 0.1f, 0.2f, 0.8f, 0, 1 }; else if (coolness == 3) colour = new float[] { 0.8f, 0, 0.8f, 0, 1 }; else if (coolness == 4) colour = new float[] { 0.5f, 0.1f, 0, 0, 1 }; float[][] ptsArray = { new float[] {0.7f, 0, 0, 0, 0}, new float[] {0, 0.7f, 0, 0, 0}, new float[] {0, 0, 0.7f, 0, 0}, new float[] {0, 0, 0, 1, 0}, colour }; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); attr.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default); } if (_imageSet != null) { e.Graphics.DrawImage(_imageSet, new Rectangle((Width / 2) - (smallish / 2), (Height / 2) - (smallish / 2), smallish, smallish), 0, 0, _imageSet.Width, _imageSet.Height, GraphicsUnit.Pixel, attr); } // for int grade draw star if (renderStars) { for (int i = 0; i < grade; i++) { e.Graphics.DrawImage(_imageStars, new Rectangle((Width / 2) - (squarish / 2) + 2 + (8 + 6 / grade) * i, (Height / 2) - (squarish / 2) + 3, 13, 13), new Rectangle(0, 0, _imageStars.Width, _imageStars.Height), GraphicsUnit.Pixel); } } } }
/// <summary> /// 画像データ生成 /// </summary> /// <param name="width">出力幅</param> /// <param name="height">出力高さ</param> /// <param name="src">元画像データ出力バッファ</param> /// <returns>結果画像</returns> public Image GenerateImages(int width, int height, out Image src) { src = Image.FromFile(Path); switch (Rotate) { case PageRotate.Rotate0: src.RotateFlip(RotateFlipType.RotateNoneFlipNone); break; case PageRotate.Rotate90: src.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case PageRotate.Rotate180: src.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case PageRotate.Rotate270: src.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } if (Format < 0) { switch (src.PixelFormat) { default: // フルカラー Format = Page.PageFormat.FullColor; break; case PixelFormat.Format8bppIndexed: // 8bitグレイスケール Format = Page.PageFormat.Gray8bit; break; case PixelFormat.Format16bppGrayScale: // 8bitグレイスケールにしちゃう Format = Page.PageFormat.Gray8bit; break; case PixelFormat.Format4bppIndexed: // 4bitグレイスケール Format = Page.PageFormat.Gray4bit; break; case PixelFormat.Format1bppIndexed: // 白黒 Format = Page.PageFormat.Mono; break; } } int srcWidth = src.Width * (100 - clipLeft - (100 - clipRight)) / 100; int srcHeight = src.Height * (100 - clipTop - (100 - clipBottom)) / 100; if (srcWidth / (double)width < srcHeight / (double)height) { double d = srcHeight / (double)height; width = (int)(srcWidth / d); } else { double d = srcWidth / (double)width; height = (int)(srcHeight / d); } if (width <= 0 || height <= 0) { return null; } // コントラスト指定 ImageAttributes attr = new ImageAttributes(); float contrast = this.contrast + 1.0f; float[][] array = { new float[] {contrast, 0, 0, 0, 0}, // red new float[] {0, contrast, 0, 0, 0}, // green new float[] {0, 0, contrast, 0, 0}, // blue new float[] {0, 0, 0, 1, 0}, // alpha new float[] {(1.0f - contrast) * 0.5f, (1.0f - contrast) * 0.5f, (1.0f - contrast) * 0.5f, 0, 1} // transform }; attr.SetColorMatrix(new ColorMatrix(array), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); attr.SetGamma(1.0f, ColorAdjustType.Bitmap); // ビットマップ生成 Image target = new Bitmap(width, height); Graphics g = Graphics.FromImage(target); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(src, new Rectangle(0, 0, width, height), src.Width * clipLeft / 100, src.Height * clipTop / 100, srcWidth, srcHeight, GraphicsUnit.Pixel, attr); g.Dispose(); if (Format > Page.PageFormat.FullColor) { // 太字化はこの段階で反映 target = ConvertFormat(Boldize((Bitmap)target, this.bold), Format); } return target; }
private static Bitmap GammaBmp( Bitmap bmpBlend, float gamma) { Bitmap bmpNew = new Bitmap(bmpBlend.Width, bmpBlend.Height); Graphics g = Graphics.FromImage(bmpNew); ImageAttributes attr = new ImageAttributes(); attr.SetGamma((float)(1.0/gamma)); g.DrawImage(bmpBlend, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmpBlend.Width, bmpBlend.Width, GraphicsUnit.Pixel, attr); g.Flush(); g.Dispose(); bmpBlend.Dispose(); return bmpNew; }
private void PostProcess(ref Bitmap originalBitmap) { const float brightness = 1.0f; const float contrast = 2.0f; const float gamma = 0.8f; const float adjustedBrightness = brightness - 1.0f; float[][] colorMatrix = { new[] {contrast, 0, 0, 0, 0}, // red new[] {0, contrast, 0, 0, 0}, // green new[] {0, 0, contrast, 0, 0}, // blue new[] {0, 0, 0, 1.0f, 0}, // leave alpha as is new[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1} }; var imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); var g = Graphics.FromImage(originalBitmap); g.DrawImage( originalBitmap, new Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height), 0, 0, originalBitmap.Width, originalBitmap.Height, GraphicsUnit.Pixel, imageAttributes ); }
/// <summary> /// Redraw image into itself using the given gamma correction. 0.1 to 5.0 are allowed. 2.0 produces no change /// </summary> /// <param name="source"></param> /// <param name="luminance"></param> public static void SetGamma(Image source, float _gamma) { // clamp to range float gamma = Math.Max(0.1f, Math.Min(_gamma, 5.0f)); // create image attributes ImageAttributes ia = new ImageAttributes(); ia.SetGamma(gamma); // get a graphics for source image Graphics g = Graphics.FromImage(source); // set compositing mode to copy over g.CompositingMode = CompositingMode.SourceCopy; // turn off aliasing etc g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; // create destination rectangle Rectangle d = new Rectangle(0, 0, source.Width, source.Height); // paint into self g.DrawImage(source, d, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia); // cleanup ia.Dispose(); g.Dispose(); }
/// <summary> /// The override for the Paint method. This method paints the graph onto the canvas. /// </summary> private void Construct() { Graphics.CompositingQuality = CompositingQuality.HighQuality; Graphics.CompositingMode = CompositingMode.SourceOver; Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; ImageAttributes ia = new ImageAttributes(); ia.SetGamma(50, ColorAdjustType.Brush); DrawingElements.Add(new DrawBackGround(Color.White, GraphCanvasRectangle)); Pen outline = new Pen(new SolidBrush(Color.DarkGray)); //DrawingElements.Add(new DrawRectangle(outline, 0, 0, Bitmap.Width - 1, Bitmap.Height - 1)); #if GRAPHDEBUG DrawingElements.Add(new DrawRectangle(debugPen, TitleRectangle)); DrawingElements.Add(new DrawRectangle(debugPen, LegendRectangle)); DrawingElements.Add(new DrawRectangle(debugPen, GraphRectangle)); #endif //graph Graphics.Clip = new Region(GraphRectangle); //adjuct the rectangle to be square if (GraphRectangle.Width > GraphRectangle.Height) GraphRectangle = new RectangleF(GraphRectangle.X + ((GraphRectangle.Width - GraphRectangle.Height)) / 2, GraphRectangle.Y, GraphRectangle.Height, GraphRectangle.Height); else GraphRectangle = new RectangleF(GraphRectangle.X, GraphRectangle.Y, GraphRectangle.Width, GraphRectangle.Width); #if GRAPHDEBUG DrawingElements.Add( new DrawString(string.Format("Width={0}, Height={1}", GraphRectangle.Width, GraphRectangle.Height), LegendFont, debugBrush, GraphRectangle, LegendStringFormat)); DrawingElements.Add(new DrawEllipse(debugPen, GraphRectangle)); #endif float total = 0; foreach (GraphRange gr in GraphData.Ranges) { total += (float)gr.SumOfValues; } float start = 0; float end = 0; float step = (360 / total); RectangleF labelRectangle = new RectangleF(GraphRectangle.X + (GraphRectangle.Width / 100 * 10), GraphRectangle.Y + (GraphRectangle.Height / 100 * 10), GraphRectangle.Width - (GraphRectangle.Width / 100 * 20), GraphRectangle.Height - (GraphRectangle.Height / 100 * 20)); RectangleF pieRectangle = new RectangleF(GraphRectangle.X + (GraphRectangle.Width / 100 * 20), GraphRectangle.Y + (GraphRectangle.Height / 100 * 20), GraphRectangle.Width - (GraphRectangle.Width / 100 * 40), GraphRectangle.Height - (GraphRectangle.Height / 100 * 40)); PointF centrePie = new PointF(pieRectangle.X + (pieRectangle.Width / 2), pieRectangle.Y + (pieRectangle.Height / 2)); ArrayList differedLabelDraw = new ArrayList(); foreach (GraphRange gr in GraphData.Ranges) { end = step * (float)gr.SumOfValues; PointF centreLabel = GraphMaths.PointOnCircleF(Convert.ToInt32(start + (end / 2)), labelRectangle); PointF edgeOfPie = GraphMaths.PointOnCircleF(Convert.ToInt32(start + (end / 2)), pieRectangle); #if GRAPHDEBUG DrawingElements.Add(new DrawLine(debugPen, centreLabel, centrePie)); DrawingElements.Add(new DrawPie(debugPen, pieRectangle, start - 90, end)); #endif DrawingElements.Add(new DrawLine(outline, centreLabel, edgeOfPie)); DrawingElements.Add( new FillPie( new LinearGradientBrush(pieRectangle, gr.PrimaryColor, gr.SecondaryColor, LinearGradientMode.ForwardDiagonal), pieRectangle, start - 90, end)); if (_textLabels == true) { DrawingElements.Add(new FillRectangle(new SolidBrush(Color.White), GraphHelpers.TextRectangleFromCentre(Graphics, gr.Label, LegendFont, centreLabel))); DrawingElements.Add( new DrawString(gr.Label, LegendFont, new SolidBrush(Color.Black), GraphHelpers.TextRectangleFromCentre(Graphics, gr.Label, LegendFont, centreLabel), null)); } else { DrawingElements.Add(new FillRectangle(new SolidBrush(Color.White), GraphHelpers.TextRectangleFromCentre(Graphics, gr.SumOfValues. ToString("F"), LegendFont, centreLabel))); DrawingElements.Add( new DrawString(gr.SumOfValues.ToString("F"), LegendFont, new SolidBrush(Color.Black), GraphHelpers.TextRectangleFromCentre(Graphics, gr.SumOfValues.ToString("F"), LegendFont, centreLabel), null)); } differedLabelDraw.Add(centreLabel); start = start + end; } Graphics.ResetClip(); //title if (DrawTitle == true) { #if GRAPHDEBUG DrawingElements.Add(new DrawRectangle(debugPen, TitleRectangle)); #endif DrawingElements.Add( new DrawString(GraphData.Title, TitleFont, new SolidBrush(TitleColor), TitleRectangle, TitleStringFormat)); } //legend if (DrawLegend == true) { //work out text height int textHeight = (int)((Graphics.MeasureString("X", LegendFont).Height) / 100 * 120); #if GRAPHDEBUG DrawingElements.Add( new DrawRectangle(debugPen, LegendRectangle.X, LegendRectangle.Y, LegendRectangle.Width - 1, GraphData.Ranges.Count*textHeight)); #endif DrawingElements.Add(new FillRectangle(new SolidBrush(Color.White), LegendRectangle.X, LegendRectangle.Y, LegendRectangle.Width - 1, GraphData.Ranges.Count * textHeight)); int lentry = 0; foreach (GraphRange gr in GraphData.Ranges) { RectangleF currentRectangle = new RectangleF(LegendRectangle.X + 20, (textHeight * lentry++) + LegendRectangle.Y, LegendRectangle.Width - 20, textHeight); RectangleF colorRectangle = currentRectangle; colorRectangle.Offset(-20, 0); colorRectangle.Width = 20; #if GRAPHDEBUG DrawingElements.Add(new DrawRectangle(debugPen, currentRectangle)); DrawingElements.Add(new DrawRectangle(debugPen, colorRectangle)); #endif string lable = gr.Label; if (lable.Length > 26) { lable = lable.Substring(0, 26); } string val = gr.SumOfValues.ToString("F"); DrawingElements.Add(new DrawString( string.Format("{0} = {1}", lable, val), LegendFont, new SolidBrush(Color.Black), currentRectangle, LegendStringFormat)); char symbol = (char)162; DrawingElements.Add(new DrawString( symbol.ToString(), new Font("Wingdings 2", 12), new SolidBrush(gr.PrimaryColor), colorRectangle, LegendStringFormat)); } DrawingElements.Add( new DrawRectangle(outline, LegendRectangle.X, LegendRectangle.Y, LegendRectangle.Width - 1, GraphData.Ranges.Count * textHeight)); } }
private void HighlightIcon() { int height = pB_info.Height; int offset = 0; Graphics g = Graphics.FromImage(pB_info.Image); g.FillRectangle(new SolidBrush(pB_info.BackColor), 0, 0, pB_info.Width, pB_info.Height); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; float brightness = 1.2f; // no change in brightness float constrast = 1.5f; // twice the contrast float gamma = 2.0f; // no change in gamma float adjustedBrightness = brightness - 1.0f; // create matrix that will brighten and contrast the image ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {constrast, 0, 0, 0, 0}, // scale red new float[] {0, constrast, 0, 0, 0}, // scale green new float[] {0, 0, constrast, 0, 0}, // scale blue new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1} }); ImageAttributes imageAttributes = new ImageAttributes(); //imageAttributes.ClearColorMatrix(); //imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); g.DrawImage(m_timerBmp, new Rectangle(offset, (pB_info.Height - height) / 2, height, height) , 0, 0, m_timerBmp.Width, m_timerBmp.Height, GraphicsUnit.Pixel, imageAttributes); g.Dispose(); pB_info.Invalidate(); }
public void setContrast() { //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[] {0, 0, 0, 0, 1f}}; ImageAttributes iA = new ImageAttributes(); //iA.ClearColorMatrix(); //iA.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); if (contrast <= 0.1f) contrast = 0.11f; iA.SetGamma(contrast); Graphics g = Graphics.FromImage(tmpImage); g.DrawImage(BaseImage, new Rectangle(0, 0, tmpImage.Width, tmpImage.Height) , 0, 0, BaseImage.Width, BaseImage.Height, GraphicsUnit.Pixel, iA); Refresh(); }
public override void Draw(Graphics g, int x, int y, Size sizTile, TemplateDoc tmpd, LayerType layer, bool fSelected) { Point ptTOrigin = GetTileOrigin(sizTile); Point ptGobOrigin = m_gimg.GetOrigin(sizTile); x += ptTOrigin.X - ptGobOrigin.X; y += ptTOrigin.Y - ptGobOrigin.Y; if (layer == LayerType.DepthSorted) { Bitmap bm = m_gimg.GetBitmapSides(sizTile)[(int)m_side]; if (fSelected) { Rectangle rcDst = new Rectangle(x, y, bm.Width, bm.Height); ImageAttributes attr = new ImageAttributes(); attr.SetGamma(0.5f); g.DrawImage(bm, rcDst, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attr); } else { g.DrawImage(bm, x, y); } } }
private static async Task<Bitmap> EnhanceImage(Bitmap originalImage) { Bitmap adjustedImage = new Bitmap(originalImage); float brightness = 1.0f; // no change in brightness float contrast = 3.5f; // 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}}; var imageAttributes = new 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); //originalImage.Save("C:\\TEMP\\ORG.PNG"); //adjustedImage.Save("C:\\TEMP\\ADJ11.PNG"); return adjustedImage; }
private void trackBar3_Scroll(object sender, EventArgs e) { label5.Text = trackBar3.Value.ToString(); gamma = 0.04f * trackBar3.Value; Bitmap bm = new Bitmap(newbitmap.Width, newbitmap.Height); Graphics g = Graphics.FromImage(bm); ImageAttributes ia = new ImageAttributes(); ia.SetGamma(gamma); g.DrawImage(newbitmap, new Rectangle(0, 0, newbitmap.Width, newbitmap.Height),0,0, newbitmap.Width , newbitmap.Height, GraphicsUnit.Pixel, ia); g.Dispose(); ia.Dispose(); pictureBox1.Image= bm; }
public Image apply(Image img) { //Program.dbg(System.Threading.Thread.CurrentThread.ManagedThreadId + ""); //Image im = (Image)img.Clone(); Image im = img; if (rotate == 1) im.RotateFlip(RotateFlipType.Rotate90FlipNone); if (rotate == 2) im.RotateFlip(RotateFlipType.Rotate180FlipNone); if (rotate == 3) im.RotateFlip(RotateFlipType.Rotate270FlipNone); if (brigh != DEFAULT_BRIGH || contr != DEFAULT_CONTR || gamma != DEFAULT_GAMMA) { using (Graphics g = Graphics.FromImage(im)) { float b = _brigh; float c = _contr; ImageAttributes derp = new ImageAttributes(); derp.SetColorMatrix(new ColorMatrix(new float[][]{ new float[]{c, 0, 0, 0, 0}, new float[]{0, c, 0, 0, 0}, new float[]{0, 0, c, 0, 0}, new float[]{0, 0, 0, 1, 0}, new float[]{b, b, b, 0, 1}})); derp.SetGamma(_gamma); g.DrawImage(img, new Rectangle(Point.Empty, img.Size), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, derp); } } //Program.dbg("Applied filters"); return im; // == null? (Image)img.Clone() : im; }
[Test] //TBD: add more overrides public void DrawImageTest() { // Create callback method. Graphics.DrawImageAbort imageCallback = new Graphics.DrawImageAbort(DrawImageCallback); IntPtr imageCallbackData = new IntPtr(1); // Create image. Image newImage = Image.FromFile("SampIcon.ico"); // Create rectangle for displaying original image. Rectangle destRect1 = new Rectangle( 100, 25, 450, 150); // Create coordinates of rectangle for source image. float x = 50.0F; float y = 50.0F; float width = 150.0F; float height = 150.0F; GraphicsUnit units = GraphicsUnit.Pixel; // Draw original image to screen. t.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units); t.Show(); // Create rectangle for adjusted image. Rectangle destRect2 = new Rectangle(100, 175, 450, 150); // Create image attributes and set large gamma. ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetGamma(4.0F); // Draw adjusted image to screen. t.Graphics.DrawImage( newImage, destRect2, x, y, width, height, units, imageAttr, imageCallback, imageCallbackData); t.Show(); }
protected override void OnPaint(PaintEventArgs e) { if (_imageSlot != null) { var attr = new System.Drawing.Imaging.ImageAttributes(); int top = Top; int bottom = Top + Height; int left = Left; int right = Left + Width; attr.SetGamma(gamma); int squarish = Math.Max(_imageSlot.Width, _imageSlot.Height); squarish = (int)(squarish * 1.25); if (renderBack) { e.Graphics.DrawImage(_imageBack, new Rectangle((Width / 2) - (squarish / 2), (Height / 2) - (squarish / 2), squarish, squarish), new Rectangle(0, 0, _imageBack.Width, _imageBack.Height), GraphicsUnit.Pixel); } //e.Graphics.DrawImage(_imageBack, (Width / 2) - (_imageBack.Width / 2), (Height / 2) - (_imageBack.Height / 2)); //e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2)); //Point[] dest = new Point[]{ new Point(top, left), new Point(top, right), new Point(bottom, right)}; e.Graphics.DrawImage(_imageSlot, new Rectangle((Width / 2) - (_imageSlot.Width / 2), (Height / 2) - (_imageSlot.Height / 2), _imageSlot.Width, _imageSlot.Height), 0, 0, _imageSlot.Width, _imageSlot.Height, GraphicsUnit.Pixel, attr); int smallish = (int)(squarish * 0.5); if (coolness != 0) { float[] colour = new float[] { 0, 0.6f, 0, 0, 1 }; if (coolness == 2) { colour = new float[] { 0.1f, 0.2f, 0.8f, 0, 1 } } ; else if (coolness == 3) { colour = new float[] { 0.8f, 0, 0.8f, 0, 1 } } ; else if (coolness == 4) { colour = new float[] { 0.5f, 0.1f, 0, 0, 1 } } ; float[][] ptsArray = { new float[] { 0.7f, 0, 0, 0, 0 }, new float[] { 0, 0.7f, 0, 0, 0 }, new float[] { 0, 0, 0.7f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, colour }; ColorMatrix clrMatrix = new ColorMatrix(ptsArray); attr.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default); } if (_imageSet != null) { e.Graphics.DrawImage(_imageSet, new Rectangle((Width / 2) - (smallish / 2), (Height / 2) - (smallish / 2), smallish, smallish), 0, 0, _imageSet.Width, _imageSet.Height, GraphicsUnit.Pixel, attr); } // for int grade draw star if (renderStars) { for (int i = 0; i < grade; i++) { e.Graphics.DrawImage(_imageStars, new Rectangle((Width / 2) - (squarish / 2) + 2 + (8 + 6 / grade) * i, (Height / 2) - (squarish / 2) + 3, 13, 13), new Rectangle(0, 0, _imageStars.Width, _imageStars.Height), GraphicsUnit.Pixel); } } } }
protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; if (this.Enabled) { // draw background /*if(base.BackgroundImage != null) g.DrawImage(base.BackgroundImage, 0, 0);*/ // draw head if(head_image != null) g.DrawImage(head_image, head_rect.X, head_rect.Y, head_image.Width, head_image.Height); } else { // disabled version of the thumb - same but dimmer float brightness = 1.0f; // no change in brightness float contrast = 0.5f; // half the contrast float gamma = 1.0f; // no change in gamma float newBrightness = brightness - 1.0f; 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[] {newBrightness, newBrightness, newBrightness, 0, 1}}; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); // draw background /*if(base.BackgroundImage != null) g.DrawImage(base.BackgroundImage, new Rectangle(0, 0, this.Width, this.Height), 0, 0, base.BackgroundImage.Width, base.BackgroundImage.Height, GraphicsUnit.Pixel, imageAttributes);*/ // draw head if(head_image != null) g.DrawImage(head_image, new Rectangle(head_rect.X, head_rect.Y, head_image.Width, head_image.Height), 0, 0, head_image.Width, head_image.Height, GraphicsUnit.Pixel, imageAttributes); imageAttributes.Dispose(); imageAttributes = null; } }
// *********************************************************************** // HELPER: DrawBitmap // INPUT : Graphics, menu item, state of the item // NOTES : Renders the bitmap for the current item taking into account the // current state of the item public virtual void DrawBitmap(Graphics g, MenuItem item, DrawItemState itemState) { // Grab the current state of the menu item //bool isSelected = (itemState & DrawItemState.Selected) != 0; bool isDisabled = (itemState & DrawItemState.Disabled) != 0; bool isChecked = (itemState & DrawItemState.Checked) != 0; Bitmap bmp = null; // Determine the bitmap to use if checked, radio-checked, normal if (isChecked == true) { if (item.RadioCheck) { bmp = (Bitmap)GetEmbeddedImage(this.RadioCheckIcon); } else { bmp = (Bitmap)GetEmbeddedImage(this.CheckIcon); } } else { if (item.RadioCheck) { bmp = (Bitmap)GetEmbeddedImage(this.RadioUnCheckIcon); } else { if (menuItemIconCollection.ContainsKey(item)) { bmp = (Bitmap)GetEmbeddedImage(menuItemIconCollection[item]); } } } // if no valid bitmap is found, exit. if (bmp == null) { return; } // Make the bitmap transparent bmp.MakeTransparent(); // Render the bitmap (the bitmap is grayed out if the // item is disabled) if (isDisabled == true) { ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetGamma(0.2F); Rectangle tmpRect = new Rectangle((int)BitmapBounds.X + 2, (int)BitmapBounds.Y + 2, (int)BitmapBounds.Width - 2, (int)BitmapBounds.Right - 2); g.DrawImage(bmp, tmpRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr); imageAttr.ClearGamma(); } else { g.DrawImage(bmp, BitmapBounds.X + 2, BitmapBounds.Y + 2); } // Free the resource. bmp.Dispose(); }
protected override void OnPaint(PaintEventArgs pevent) { var g = pevent.Graphics; g.SetClip(pevent.ClipRectangle); g.Clear(_bgColor); using (var b = new SolidBrush(Color.FromArgb(130, Color.White))) { g.FillRectangle(b, new Rectangle(0, 0, Width, Height / 2)); } if (Image != null) { var limitW = Width * 0.8f; var limitH = Height * 0.8f; var scale = 1.0f; SizeF newSize = Image.Size; if (newSize.Width >= limitW) { scale = limitW / newSize.Width; newSize.Width = limitW; newSize.Height *= scale; } if (newSize.Height >= limitH) { scale = limitH / newSize.Height; newSize.Height = limitH; newSize.Width *= scale; } if (Enabled) { g.DrawImage(Image, (Width - newSize.Width) / 2, (Height - newSize.Height) / 2, newSize.Width, newSize.Height); } else { using (var attr = new ImageAttributes()) { attr.SetGamma(0.2f); g.DrawImage(Image, new Rectangle(new Point((int) ((Width - newSize.Width) / 2), (int) ((Height - newSize.Height) / 2)),Size.Round(newSize)), 0,0,Image.Width,Image.Height,GraphicsUnit.Pixel,attr); } } } if (Text != null && Text.Length > 0) { var txtSize = g.MeasureString(Text, Font); using (var b = new SolidBrush(Enabled?ForeColor:Color.DarkGray)) { g.DrawString(Text, Font, b, new Point((int)((Width - txtSize.Width) / 2), (int)((Height - txtSize.Height) / 2))); } } //g.DrawRectangle(Pens.WhiteSmoke,new Rectangle(1,1,Width-2,Height-2)); if(Enabled) g.DrawRectangle(Pens.Gray, new Rectangle(0, 0, Width - 1, Height - 2)); else g.DrawRectangle(Pens.DarkGray, new Rectangle(0, 0, Width - 1, Height - 2)); g.DrawLine(Pens.White, 0, Height - 1, Width, Height - 1); }
/// <summary> /// Change the intensity of the image /// </summary> /// <param name="image">The dot image</param> /// <param name="weight">The weight to apply</param> /// <returns>The weighted image</returns> private static Bitmap ApplyHeatValueToImage(Bitmap image, float weight) { var tempImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb); using (IGraphics g = Graphics.FromImage(tempImage).G()) { //I want to make the color more intense (White/bright) if (weight < 0.02f) weight = 0.02f; weight *= 5f; if (weight > 5f) weight = 5f; // Create ImageAttributes var ia = new ImageAttributes(); //Gamma values range from 0.1 to 5.0 (normally 0.1 to 2.2), with 0.1 being the brightest and 5.0 the darkest. //Convert the 100% to a range of 0.1-5 by multiplying it by 5 ia.SetGamma(weight, ColorAdjustType.Bitmap); // Draw Image with the attributes g.DrawImage(image, 0, 0, image.Width, image.Height, 0, 0, image.Width, image.Height, GraphicsUnitType.Pixel, ia); } //New dot with a different intensity return tempImage; }
private void redrawSamplePictureBox() { // create the image if not already existing if (this.samplePictureBox.Image == null) this.samplePictureBox.Image = new Bitmap(this.samplePictureBox.Width, this.samplePictureBox.Height); // get the gc of the image Graphics graphics = Graphics.FromImage(this.samplePictureBox.Image); // set the background color graphics.Clear(backgroundColorPictureBox.BackColor); // start corner for the grid int startGridCoord = 10; // draw some sub-grid lines Pen linePen = new Pen(subGridColorPictureBox.BackColor, 1); for (int i = startGridCoord - 8; i < this.samplePictureBox.Height; i += 8) { graphics.DrawLine(linePen, 0, i, this.samplePictureBox.Width, i); graphics.DrawLine(linePen, i, 0, i, this.samplePictureBox.Height); } // draw some grid lines linePen = new Pen(gridColorPictureBox.BackColor, 2); for (int i = startGridCoord; i < this.samplePictureBox.Height; i += 32) { graphics.DrawLine(linePen, 0, i, this.samplePictureBox.Width, i); graphics.DrawLine(linePen, i, 0, i, this.samplePictureBox.Height); } // create the image attributes ImageAttributes imageAttributeForSelection = new ImageAttributes(); imageAttributeForSelection.SetGamma(getGammaFromNumericUpDown(this.GammaForSelectionNumericUpDown)); ImageAttributes imageAttributeForSnapping = new ImageAttributes(); imageAttributeForSnapping.SetGamma(getGammaFromNumericUpDown(this.GammaForSnappingNumericUpDown)); // get the part example image from the resource Image image = Properties.Resources.PartForOptionPreview; // draw 3 part images as an example Rectangle destinationRectangle = new Rectangle(42, 42, image.Width, image.Height); graphics.DrawImage(image, destinationRectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); destinationRectangle.Y += 32; graphics.DrawImage(image, destinationRectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributeForSelection); destinationRectangle.Y += 32; graphics.DrawImage(image, destinationRectangle, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributeForSnapping); // invalidate the picture box this.samplePictureBox.Invalidate(); }
// Adjust brightness contrast and gamma of an image private Bitmap imgBalance(Bitmap img, int brigh, int cont, int gam) { lblStatus.Text = "Balancing..."; Refresh(); ImageAttributes imageAttributes; float brightness = (brigh/100.0f) + 1.0f; float contrast = (cont/100.0f) + 1.0f; float gamma = 1/(gam/100.0f); float adjustedBrightness = brightness - 1.0f; Bitmap output; // create matrix that will brighten and contrast the image float[][] ptsArray = { new[] {contrast, 0, 0, 0, 0}, // scale red new[] {0, contrast, 0, 0, 0}, // scale green new[] {0, 0, contrast, 0, 0}, // scale blue new[] {0, 0, 0, 1.0f, 0}, // don't scale alpha new[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1} }; output = new Bitmap(img); imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); imageAttributes.SetGamma(gamma, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(output); g.DrawImage(output, new Rectangle(0, 0, output.Width, output.Height) , 0, 0, output.Width, output.Height, GraphicsUnit.Pixel, imageAttributes); lblStatus.Text = "Done"; Refresh(); return (output); }
/// <summary>Adjust gamma of an image [not used]</summary> private void DrawBrightImage(Graphics g, Image img, Rectangle bounds, float gamma) { try { using (Bitmap buttonImage = new Bitmap(img)) { using (ImageAttributes imageAttr = new ImageAttributes()) { if (gamma > .9f) gamma = .9f; if (gamma < .2f) gamma = .2f; // raise gamma imageAttr.SetGamma(gamma); g.DrawImage(buttonImage, bounds, 0, 0, buttonImage.Width, buttonImage.Height, GraphicsUnit.Pixel, imageAttr); } } } catch { } }
/// <summary> /// Gamma校正 /// </summary> /// <param name="bmp"> 待处理的图像 </param> /// <param name="value"> Gamma值 </param> /// <returns> Gamma校正后的图像 </returns> public static Bitmap Gamma(this Bitmap bmp, float value) { if (Equals(value, 1.0000f)) { return bmp; } Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height); Graphics graphics = Graphics.FromImage(newBmp); ImageAttributes attribtues = new ImageAttributes(); attribtues.SetGamma(value, ColorAdjustType.Bitmap); graphics.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attribtues); graphics.Dispose(); return newBmp; }
/// <summary> /// Create ImageAttributes to modify /// </summary> /// <param name="brightness"></param> /// <param name="contrast"></param> /// <param name="gamma"></param> /// <returns>ImageAttributes</returns> public static ImageAttributes CreateAdjustAttributes(float brightness, float contrast, float gamma) { float adjustedBrightness = brightness - 1.0f; ColorMatrix applyColorMatrix = new ColorMatrix( new float[][] { 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} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); attributes.ClearColorMatrix(); attributes.SetColorMatrix(applyColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); attributes.SetGamma(gamma, ColorAdjustType.Bitmap); return attributes; }