Compare two Color objects

Always read the documentation first: “To compare colors based solely on their ARGB values, you should use the ToArgb method. This is because the Equals and Equality members determine equivalency using more than just the ARGB value of the colors. For example, Black and FromArgb(0,0,0) are not considered equal, since Black is a named color … Read more

Double buffering with Panel

Use a PictureBox if you don’t need scrolling support, it is double-buffered by default. Getting a double-buffered scrollable panel is easy enough: using System; using System.Windows.Forms; class MyPanel : Panel { public MyPanel() { this.DoubleBuffered = true; this.ResizeRedraw = true; } } The ResizeRedraw assignment suppresses a painting optimization for container controls. You’ll need this … Read more

how to draw a line on a image?

“Draw a line on a bmp image which is pass into a method using drawline method in C#” PaintEventArgs e would suggest that you are doing this during the “paint” event for an object. Since you are calling this in a method, then no you do not need to add PaintEventArgs e anywhere. To do … Read more

Cannot find Bitmap Class in Class Library (.NET Standard)

I’m the author of CoreCompat.System.Drawing. If you’re on .NET Core 2.0, I’d recommend you’d move to System.Drawing.Common instead, which is the Microsoft-maintained implementation of System.Drawing for .NET Core. If you’re on Linux or macOS, make sure to install libgdiplus. On macOS, run brew install mono-libgdiplus; on Linux your package manager should provide you with a … Read more

File.Delete failing when Image.FromFile was called prior it, despite making copy of loaded image and destroying original one

This should do the trick: Image img = null; using (var stream = File.OpenRead(path)) { img = Image.FromStream(stream); } File.Delete(path); UPDATE: Don’t use the code above! I’ve found the related knowledge base article: http://support.microsoft.com/?id=814675 The solution is to really copy the bitmap as outlined in the article. I’ve coded the two ways that the article … Read more

Manipulating images with .NET Core

Disclaimer: This is my software. I’m working on a cross-platform 2D Graphics library that runs on .NET Core It’s currently alpha but already supports a comprehensive feature set. https://github.com/JimBobSquarePants/ImageSharp Example usage. using (FileStream stream = File.OpenRead(“foo.jpg”)) using (FileStream output = File.OpenWrite(“bar.jpg”)) { Image image = new Image(stream); image.Resize(image.Width / 2, image.Height / 2) .Greyscale() .Save(output); … Read more

System.Drawing Out of Memory Exception

I’ve seen System.Drawing throw OutOfMemoryExceptions even when it’s not out of memory. Some GDI+ function is apparently just returning a stupid error code. IIRC, you will get an OutOfMemoryException if you try to use a LinearGradientBrush to fill a rectangle whose width or height is zero. There may be other conditions too, but this is … Read more