Image.Save creates empty/blank file

Jul 20, 2020
Programming Experience
Okay, so I got a real weirdo here, and web searches and MSDN haven't been much help. Here's a minimal version of my code:

using System.Drawing;

// Create an empty image
var bmp = new Bitmap(156, 128);
var graphics = Graphics.FromImage(bmp);

// Draw on it
var blue = new SolidBrush(Color.FromArgb(0, 0, 45, 98));
var bgRect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
graphics.FillRectangle(blue, bgRect);

// And here comes the bug: Save here decides it wants a blank/empty image because {reasons?}
// Same bug with bmp or png formats
bmp.Save("temp.png", System.Drawing.Imaging.ImageFormat.Png);

Now having worked with other drawing tools (SDL, for example), my best guess is there must be some "Blit" or "Render" or "Update" step that I'm missing (kind of like SDL_RenderUpdate or SDL_BlitSurface). Like okay, we start out with a blank image, then create an object off of that, then did some stuff with the object, but do we ever actually do anything with the image. I've read there's some kind of "image encoder" object; maybe I need to create one of those and see what it can do? There is no "Graphics.BackToImage" so sadly no obvious stuff like that. This is one of those times I with .NET were more procedural lol - let data be data and actions be actions, no weird "oh, which object do I have to create, and which of its public method do I have to call, to make this stupid goober work?" lol idk... any ideas?


Staff member
Apr 6, 2019
Chesapeake, VA
Programming Experience
The first parameter of FromArgb() is the alpha channel. A value of zero means completely transparent.
Top Bottom