/// <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 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; }
public static Image Apply(this ColorMatrix matrix, Image src, Image dest, Rectangle destRect) { using (Graphics g = Graphics.FromImage(dest)) using (ImageAttributes ia = new ImageAttributes()) { ia.ClearColorMatrix(); ia.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.SetHighQuality(); g.DrawImage(src, destRect, 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, ia); } return dest; }
/// <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; }
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; } }
/// <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; }
/// <summary> /// Apply a color matrix by copying from the source to the destination /// </summary> /// <param name="source">Image to copy from</param> /// <param name="sourceRect">Rectangle to copy from</param> /// <param name="destRect">Rectangle to copy to</param> /// <param name="dest">Image to copy to</param> /// <param name="colorMatrix">ColorMatrix to apply</param> public static void ApplyColorMatrix(Bitmap source, Rectangle sourceRect, Bitmap dest, Rectangle destRect, ColorMatrix colorMatrix) { using (ImageAttributes imageAttributes = new ImageAttributes()) { imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(colorMatrix); ApplyImageAttributes(source, sourceRect, dest, destRect, imageAttributes); } }
public bool ToWaterMark() { Exception exception; Random random; string str3; int num10; int num11; FileHelper helper = new FileHelper(); string str = (this.SaveWaterMarkImagePath == null) ? this.SourceImagePath : this.SaveWaterMarkImagePath; string sExt = this.SourceImagePath.Substring(this.SourceImagePath.LastIndexOf(".")).ToLower(); if (this.SourceImagePath.ToString() == string.Empty) { throw new NullReferenceException("SourceImagePath is null!"); } if (!this.CheckValidExt(sExt)) { this.message = "原图片文件格式不正确,支持的格式有[ " + this.AllowExt + " ]"; return false; } FileStream stream = File.OpenRead(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); Image original = Image.FromStream(stream, true); stream.Close(); int width = original.Width; int height = original.Height; float horizontalResolution = original.HorizontalResolution; float verticalResolution = original.VerticalResolution; Bitmap image = new Bitmap(original, width, height); original.Dispose(); image.SetResolution(72f, 72f); Graphics graphics = Graphics.FromImage(image); try { if ((this.WaterMarkText != null) && (this.WaterMarkText.Trim().Length > 0)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel); int[] numArray = new int[] { 0x10, 14, 12, 10, 8, 6, 4 }; Font font = null; SizeF ef = new SizeF(0f, 0f); for (int i = 0; i < numArray.Length; i++) { font = new Font("arial", (float) numArray[i], FontStyle.Bold); ef = graphics.MeasureString(this.WaterMarkText, font); if (((ushort) ef.Width) < ((ushort) width)) { break; } } numArray = null; float y = (height - ((int) (height * 0.05000000074505806))) - (ef.Height / 2f); float x = width / 2; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; graphics.DrawString(this.WaterMarkText, font, new SolidBrush(Color.FromArgb(0x99, 0, 0, 0)), new PointF(x + 1f, y + 1f), format); graphics.DrawString(this.WaterMarkText, font, new SolidBrush(Color.FromArgb(this.Diaphaneity, 0xff, 0xff, 0xff)), new PointF(x, y), format); format.Dispose(); } } catch (Exception exception1) { exception = exception1; this.message = exception.Message; } finally { graphics.Dispose(); } if ((this.WaterMarkImagePath == null) || (this.WaterMarkImagePath.Trim() == string.Empty)) { random = new Random(); str3 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; string str4 = HttpContext.Current.Server.MapPath("~/" + str); image.Save(str4 + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } Image image2 = Image.FromFile(HttpContext.Current.Server.MapPath("~/" + this.WaterMarkImagePath)); int num8 = image2.Width; int num9 = image2.Height; if ((width < num8) || (height < (num9 * 2))) { random = new Random(); str3 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; image.Save(HttpContext.Current.Server.MapPath("~/" + str) + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } Bitmap bitmap2 = new Bitmap(image); image.Dispose(); bitmap2.SetResolution(horizontalResolution, verticalResolution); Graphics graphics2 = Graphics.FromImage(bitmap2); ImageAttributes imageAttr = new ImageAttributes(); ColorMap map = new ColorMap(); map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0); map.NewColor = Color.FromArgb(0, 0, 0, 0); imageAttr.SetRemapTable(new ColorMap[] { map }, ColorAdjustType.Bitmap); float[][] newColorMatrix = new float[5][]; float[] numArray3 = new float[5]; numArray3[0] = 1f; newColorMatrix[0] = numArray3; numArray3 = new float[5]; numArray3[1] = 1f; newColorMatrix[1] = numArray3; numArray3 = new float[5]; numArray3[2] = 1f; newColorMatrix[2] = numArray3; numArray3 = new float[5]; numArray3[3] = this.ImageDeaphaneity; newColorMatrix[3] = numArray3; numArray3 = new float[5]; numArray3[4] = 1f; newColorMatrix[4] = numArray3; imageAttr.SetColorMatrix(new ColorMatrix(newColorMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); this.GetWaterMarkXY(out num10, out num11, width, height, num8, num9); graphics2.DrawImage(image2, new Rectangle(num10, num11, num8, num9), 0, 0, num8, num9, GraphicsUnit.Pixel, imageAttr); imageAttr.ClearColorMatrix(); imageAttr.ClearRemapTable(); image2.Dispose(); graphics2.Dispose(); try { random = new Random(); str3 = "YXShop" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + random.Next().ToString() + sExt; bitmap2.Save(HttpContext.Current.Server.MapPath("~/" + str) + @"\" + str3); helper.DeleteFile(HttpContext.Current.Server.MapPath("~/" + this.SourceImagePath)); if (str.Substring(0, 1) == "/") { str = str.Substring(1, str.Length - 1); } this.filePath = str + "/" + str3; return true; } catch (Exception exception2) { exception = exception2; this.message = exception.Message; } finally { bitmap2.Dispose(); } return false; }
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 ); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); curval = (curval + nextval) / 2; curpos = (curpos + nextpos) / 2; cursel = (cursel + nextsel) / 2; curact = (curact + nextact) / 2; curanim = (curanim + nextanim) / 2f; if (curtick == maxcoltick) { Random r = new Random(); nred = r.Next(255); ngreen = r.Next(255); nblue = r.Next(255); rred = (nred - cred) / maxcoltick; rgreen = (ngreen - cgreen) / maxcoltick; rblue = (nblue - cblue) / maxcoltick; curtick = 0; } curtick++; cred += rred; cgreen += rgreen; cblue += rblue; Rectangle rect = e.ClipRectangle; Bitmap b = new Bitmap(rect.Width, rect.Height); Graphics g = Graphics.FromImage(b); Rectangle q = rect; q.Inflate(-50, -50); Brush RandomColorBG = new SolidBrush(Color.FromArgb((int)Clamp(cred + 32, 0, 255), (int)Clamp(cgreen + 32, 0, 255), (int)Clamp(cblue + 32, 0, 255))); if(loaded) if(currentSong.HasArt) e.Graphics.DrawImage(currentSong.art, q); else e.Graphics.FillRectangle(colorMode == ColorMode.System ? new SolidBrush(SysColor) : (colorMode == ColorMode.Image ? new SolidBrush(SongColor) : RandomColorBG), q); Rectangle c = rect; if (playing) { float per = curval > 1 ? 1 : curval; c.Inflate(-50, -50); c.Inflate((int)(50 * per), (int)(50 * per)); } g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; Brush RandomColor = new SolidBrush(Color.FromArgb((int)cred, (int)cgreen, (int)cblue)); g.FillEllipse(colorMode == ColorMode.System ? new SolidBrush(SysColor) : (colorMode == ColorMode.Image ? new SolidBrush(SongColor) : RandomColor), rect); g.FillPie(new SolidBrush(Color.FromArgb(128, (SongColor.R + SongColor.G + SongColor.B) / 3 < 32 && colorMode != ColorMode.Random ? Color.White : Color.Black)), c, 270f, curpos * 360f); StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Center; strfmt.LineAlignment = StringAlignment.Center; if (selecting) { if (loaded && !moving && !inCenter && currentSong.HasArt) e.Graphics.FillRectangle(InnerBrush_Image, q); if (moving) { if (mode == selectionModes.Position) { g.FillPie(new SolidBrush(Color.FromArgb(128, Color.LightGray)), rect, 270f, cursel); } if (mode == selectionModes.Volume) { g.FillPie(new SolidBrush(Color.FromArgb(128, Color.Black)), rect, 270f, curact); g.FillPie(new SolidBrush(Color.FromArgb(64, Color.LightGray)), rect, 270f, cursel); } if (mode == selectionModes.Track) { e.Graphics.FillRectangle(InnerBrush_Image, q); nextact = NormalizeDegrees((((float)Math.Floor((cursel / 360f) * currentPlaylist.Songs.Count) / currentPlaylist.Songs.Count) * 360f) - 90f); g.FillPie(new SolidBrush(Color.FromArgb(128, Color.Black)), rect, curact, (360f / currentPlaylist.Songs.Count)); e.Graphics.DrawString(currentPlaylist.Songs[(int)((nextsel / 360f) * currentPlaylist.Songs.Count)].title, SongInfoTitleFont, Brushes.Black, new RectangleF(q.Location, q.Size), strfmt); } } } if (playlistActive) menucount = Labels.Length; else menucount = LabelsNoPlaylist.Length; if(curact > 0.5 && !moving){ for (int i = 0; i < menucount; i++) { g.FillPie(new SolidBrush(Color.FromArgb(64, Color.White)), rect, 270f + (i * curact), curact); g.DrawPie(new Pen(Color.FromArgb(172, Color.White), 2), rect, 270f + (i * curact), curact); } } g.FillEllipse(Brushes.Lime, q); ImageAttributes ia = new ImageAttributes(); ia.SetColorKey(Color.Green, Color.Lime); ColorMatrix matrix = new ColorMatrix(); matrix.Matrix33 = curanim; ia.SetColorMatrix(matrix); if (loaded && (mouseInside || (!moving && mode != selectionModes.Track)) && (selecting ? inCenter : true)) { e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb((int)(128 * curanim), Color.White)), q); e.Graphics.DrawImage(SongInformation, new Rectangle(50, 50, 200, 200), 0f, 0f, 200f, 200f, GraphicsUnit.Pixel, ia); } ia.ClearColorMatrix(); e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; e.Graphics.DrawImage(b, rect, rect.X, rect.Y, rect.Width, rect.Height, GraphicsUnit.Pixel, ia); if (loaded && selecting && !moving && !inCenter) { e.Graphics.DrawString(playlistActive ? Labels[(int)(nextsel / (360f / menucount))] : LabelsNoPlaylist[(int)(nextsel / (360f / menucount))], MenuSelectionFont, new SolidBrush(Color.FromArgb((int)(255 * curanim), Color.Black)), q, strfmt); } g.Dispose(); b.Dispose(); }
/// <summary> /// Adjust the brightness, contrast or gamma of the given image. /// </summary> /// <param name="originalImage">The original image.</param> /// <param name="brightness">The brightness multiplier (-1.99 to 1.99 fully white).</param> /// <param name="contrast">The contrast multiplier (2.0 would be twice the contrast).</param> /// <param name="gamma">The gamma multiplier (1.0 would no change in gamma).</param> /// <returns>A new adjusted image.</returns> private static Bitmap ImageAdjust( Bitmap originalImage, float brightness = 1.0f, float contrast = 1.0f, float gamma = 1.0f ) { Bitmap adjustedImage = originalImage; float adjustedBrightness = brightness - 1.0f; // Matrix used to effect 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 }, // no change to 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 ); return adjustedImage; }
//------------------------------------------------------------------------------------------------ //Kontrast berechnen private void kontrastBerechnen() { Bitmap neueBitmap = new Bitmap(kontrastPicturebox.Image.Width, kontrastPicturebox.Image.Height); kontrastPicturebox.Image = null; GC.Collect(); Debug.WriteLine("Verarbeiteter Wert: " + kontrastwert); float angepassteHelligkeit = (1.0f - kontrastwert) / 2.0f; float[][] meineMatrix ={ new float[] {kontrastwert, 0, 0, 0, 0}, new float[] {0, kontrastwert, 0, 0, 0}, new float[] {0, 0, kontrastwert, 0, 0}, new float[] {0, 0, 0, 1.0f, 0}, new float[] {angepassteHelligkeit,angepassteHelligkeit,angepassteHelligkeit, 0, 1}}; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(meineMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(neueBitmap); g.DrawImage(parent.getPictureBoxImage(), new Rectangle(0, 0, neueBitmap.Width, neueBitmap.Height), 0, 0, neueBitmap.Width, neueBitmap.Height, GraphicsUnit.Pixel, imageAttributes); kontrastPicturebox.Image = neueBitmap; }
public static BitmapImage CustomColorSettings(BitmapImage targetBitmap, float brightness = 1f, float contrast = 1.0f, float gamma = 1.0f) { if (brightness != 0) brightness /= 100; if (contrast != 0) contrast /= 100; if (gamma != 0) gamma /= 100; Bitmap originalImage = ConvertBitmapImageToBitmap(targetBitmap); Bitmap adjustedImage = new Bitmap(originalImage); float adjustedBrightness = brightness - 1.0f; float[][] ptsArray ={ 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.0f, 0}, 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(adjustedImage, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height) , 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel, imageAttributes); BitmapImage outputBitmapImage = ConvertBitmapToBitmapImage(adjustedImage); return outputBitmapImage; }
//------------------------------------------------------------------------------------------------ //Helligkeit berechnen private void helligkeitberechnen() { Bitmap neueBitmap = new Bitmap(helligkeitPicturebox.Image.Width, helligkeitPicturebox.Image.Height); //Vorigen Bearbeitungsschritt löschen helligkeitPicturebox.Image = null; GC.Collect(); float[][] meineMatrix ={ 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.0f, 0}, new float[] {helligkeitParameter,helligkeitParameter,helligkeitParameter, 0, 1}}; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(meineMatrix), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(neueBitmap); g.DrawImage(parent.getPictureBoxImage(), new Rectangle(0, 0, neueBitmap.Width, neueBitmap.Height), 0, 0, neueBitmap.Width, neueBitmap.Height, GraphicsUnit.Pixel, imageAttributes); helligkeitPicturebox.Image = neueBitmap; }
private Bitmap schnelleKonrastFkt(float contrast_in, Bitmap original) { contrast_in += (float)kontrastwert; kontrastwert = contrast_in; Bitmap adjustedImage = new Bitmap(original.Width, original.Height); float brightness = 1.0f; // no change in brightness float kontrast = contrast_in; //double gamma = 1.0f; // no change in gamma //float adjustedBrightness = brightness - 1.0f; float adjustedBrightness = (1.0f - kontrast) / 2.0f; // create matrix that will brighten and contrast the image float[][] ptsArray ={ new float[] {kontrast, 0, 0, 0, 0}, // scale red new float[] {0, kontrast, 0, 0, 0}, // scale green new float[] {0, 0, kontrast, 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.SetGamma(gamma, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(adjustedImage); g.DrawImage(original, new Rectangle(0, 0, adjustedImage.Width, adjustedImage.Height) , 0, 0, adjustedImage.Width, adjustedImage.Height, GraphicsUnit.Pixel, imageAttributes); return adjustedImage; }
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; }
// 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); }
private static Image AdjustBrightness(int brightness, Image image) { var newImage = new Bitmap(image.Picture); Bitmap clonedImage = newImage; float adjustedBrightness = (float)brightness / 255.0f; // create matrix that will brighten and contrast the image float[][] ptsArray ={ new float[] {1, 0, 0, 0, 0}, // scale red new float[] {0, 1, 0, 0, 0}, // scale green new float[] {0, 0, 1, 0, 0}, // scale blue new float[] {0, 0, 0, 1, 0}, new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 1, 1}}; var imageAttributes = new ImageAttributes(); imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // Copy back to the original image from the cloned image Graphics g = Graphics.FromImage(newImage); g.DrawImage(clonedImage, new Rectangle(0, 0, clonedImage.Width, clonedImage.Height) , 0, 0, clonedImage.Width, clonedImage.Height, GraphicsUnit.Pixel, imageAttributes); g.Flush(); using (MemoryStream memoryStream = new MemoryStream()) { newImage.Save(memoryStream, ImageFormat.Png); newImage = new Bitmap(memoryStream); } var sabotagedImage = image.Copy(); sabotagedImage.Picture = newImage; return sabotagedImage; }