C# saving a bitmap results in generic GDI+ error

If you save a bitmap the following exception can pop up: System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) Possible causes: You tried to save the file with an invalid filename or in a directory that doesn’t exist You constructed a bitmap from a memorystream that has been […]

C# Scale bitmap

The following code scales a bitmap from the filesystem to a defined width: private static void ResizeBitmap(string filename) { using (Bitmap source = new Bitmap(filename)) { const int width = 1024; int height = (int) (((double)width/source.Width)*source.Height); using (Bitmap result = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(result)) { g.CompositingQuality = CompositingQuality.HighQuality; g.PixelOffsetMode = […]

PInvokeStackImbalance Excpetion

If you get a PInvokeStackImbalance exception it could be: One parameter has a wrong type (p. x. int changed to long) The CallingConvention of the DllImport. Change it in the declaration: [DllImport(“user32.dll”, EntryPoint=”SetWindowLongA”, SetLastError=true)] private static extern long SetWindowLong (IntPtr hwnd, int nIndex, long dwNewLong); for example to [DllImport(“user32.dll”, EntryPoint=”SetWindowLongA”, SetLastError=true, CallingConvention = CallingConvention.Cdecl)] private […]