Fast rendering images in c# – part 2

January 22, 2008

As I reported in  previous part (fast drawing images using bitblt function), using this technique you actually can draw images about 8 times faster than using standard Graphics.DrawImage method. However, if you tried to do that, you probably noticed that the image is copied with its background, as is. Sometimes this result is not desired. For example when you have some symbol and string near it, moving the whole bitmap from memory to screen will move also all not used space between picture and string. Even if you not specified any background color, still blue color (default background) will be copied. Is it possible to avoid that? The good news are – you can. As my friend Michael Kogan pointed, there is another WINAPI function, called TransparentBlt. Its definition in C# look as the following:

[DllImport("Msimg32.dll")]
public static extern bool TransparentBlt(IntPtr hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int hHeightDest, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
int crTransparent // color to make transparent
);

This function looks very similiar to BitBlt, and you should use it in the same way exactly (selectObject and all the other staff). You have working example to that in the previous part. There are 2 important differences between these functions: first, there is no raster operation here. It always using SourceCopy operation – just coping from DC to DC. Maybe you can use SetROP2 function – didn’t tryed yet, please let me know if it works. Second, last argument is crTransparent. That is exactly the color, which will be NOT copied from source. You should provide here Win32 color representation (COLORREF) structure. To convert Color object to COLORREF, use this:
int transparentColor = ColorTranslator.ToWin32(Color.Black);

Of cause, the color argument should be as the background color of the source image. As I mentioned before, it works and that is the good news. The bad news is that TransparentBlt slower than BitBlt. For 10000 drawings, BitBlt takes about 1100ms, when TransparentBlt takes about 1700ms. I will write here in case I discover something faster.

Entry Filed under: General C#. .

1 Comment Add your own

  • 1. Calciu Sorin  |  June 28, 2008 at 9:14 am

    What about drawing real transparent images using BitBlt?
    I mean every color should be computed with an alpha transparency instead of using a single background transparent color.

    Reply

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

Top Posts

Tags

.NET addin app.config ArrayList bug CAB Configuration ConfigurationManager ConfigurationSection ContentControl ContextMenu CTime; DateTime custom keys DataBinding DataContext Data templates debugging equals gethashcode GUI Hashtable interlocked Invoke lock lock free memcpy MFC multithreading multithreading; lock free override performance SCSF serialization Smart Client Software Factory Styles System.Configuration unsafe virtual functions Visual Studio wait free WinAPI WinForms WinForms\WPF Integration World of Warcraft World of Warcraft; Addon

Archives