How to apply a fade transition effect to Images using a Timer?

There are some problem to fix here: → fadeTimer.Interval = 10;: You’re (possibly) using the wrong Timer: the System.Timers.Timer‘s Elapsed is raised in a ThreadPool Thread. Unless you have set the SynchronizingObject to a Control object, which is then used to marshal the handler calls, referencing a Control in the handler will cause trouble (cross-thread … Read more

How to debug GDI Object Leaks?

It is pretty rare to exceed the 10,000 object limit for GDI objects only, the garbage collector will take care of them when you don’t call their Dispose() method yourself. A much more likely failure mode is exceeding the object limit for windows. Which is very easy to do in Winforms, Controls.Clear() or Controls.Remove() will … Read more

How can I take a screenshot and save it as JPEG on Windows?

OK, after a lot of effort here’s the answer: int SaveJpeg(HBITMAP hBmp, LPCWSTR lpszFilename, ULONG uQuality) { ULONG *pBitmap = NULL; CLSID imageCLSID; EncoderParameters encoderParams; int iRes = 0; typedef Status (WINAPI *pGdipCreateBitmapFromHBITMAP)(HBITMAP, HPALETTE, ULONG**); pGdipCreateBitmapFromHBITMAP lGdipCreateBitmapFromHBITMAP; typedef Status (WINAPI *pGdipSaveImageToFile)(ULONG *, const WCHAR*, const CLSID*, const EncoderParameters*); pGdipSaveImageToFile lGdipSaveImageToFile; // load GdipCreateBitmapFromHBITMAP lGdipCreateBitmapFromHBITMAP = … Read more

Winforms: SuspendLayout/ResumeLayout is not enough?

We’ve seen this problem too. One way we’ve seen to “fix” it is to completely suspend drawing of the control until we’re ready to go. To accomplish this, we send the WM_SETREDRAW message to the control: // Note that WM_SetRedraw = 0XB // Suspend drawing. UnsafeSharedNativeMethods.SendMessage(handle, WindowMessages.WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero); … // Resume drawing. UnsafeSharedNativeMethods.SendMessage(handle, WindowMessages.WM_SETREDRAW, … Read more

InvalidOperationException – object is currently in use elsewhere – red cross

This is because Gdi+ Image class is not thread safe. Hovewer you can avoid InvalidOperationException by using lock every time when you need to Image access, for example for painting or getting image size: Image DummyImage; // Paint lock (DummyImage) e.Graphics.DrawImage(DummyImage, 10, 10); // Access Image properties Size ImageSize; lock (DummyImage) ImageSize = DummyImage.Size; BTW, … Read more

How do I adjust the brightness of a color?

As a simple approach, you can just factor the RGB values: Color c1 = Color.Red; Color c2 = Color.FromArgb(c1.A, (int)(c1.R * 0.8), (int)(c1.G * 0.8), (int)(c1.B * 0.8)); (which should darken it; or, for example, * 1.25 to brighten it)

Saving image to file

You could try to save the image using this approach SaveFileDialog dialog=new SaveFileDialog(); if (dialog.ShowDialog()==DialogResult.OK) { int width = Convert.ToInt32(drawImage.Width); int height = Convert.ToInt32(drawImage.Height); using(Bitmap bmp = new Bitmap(width, height)) { drawImage.DrawToBitmap(bmp, new Rectangle(0, 0, width, height)); bmp.Save(dialog.FileName, ImageFormat.Jpeg); } }