Click here to Skip to main content
13,860,973 members
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 15 Nov 2006

Fast Drawing of Non-32bpp Images with System.Drawing

, 5 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Avoid the unnecessary pixel format conversion that normally happens when drawing a portion of an image to the screen with System.Drawing


It is a well-known fact that when drawing images to the screen that are a different pixel format than the screen's pixel format, format conversion must be performed. GDI+ provides the CachedBitmap class to facilitate easy caching of a converted version of a bitmap. However, this functionality is not exposed in .NET, so normally bitmaps that do not match the screen format are converted on each drawing call.

The project

At first glance, it would seem that it shouldn't be too performance-degrading, even for large images, to just draw the area of the bitmap that is exposed or invalidated using Graphics.DrawImage(). However, a further performance examination reveals that the speed of Graphics.DrawImage() is always related to the size of the whole bitmap, not the size of the area being drawn. If you tell GDI+ to draw even a 1-pixel-by-1-pixel section of a bitmap that is not in the same pixel format as the screen, it will convert the entire bitmap to the screen pixel format before completing the drawing call!

This is clearly unacceptable, especially since digital camera images are typically 24bpp. So, I set out to write a routine that would create a new bitmap that is the size of the area that needs to be drawn. It would then copy the correct section of the source bitmap into that area and draw that to the screen. This would give much better performance.

However, I also wanted to see if I could make GDI+ convert only the relevant area of the bitmap directly, rather than having the overhead of the extra copying routine. I realized that with my <a href="/KB/graphics/pointerlessimageproc.asp">EditableBitmap</a> class, I could create an EditableBitmap as a view on another EditableBitmap's bits. Then I could just report to GDI+ that the "stride" is the size of the source bitmap's stride. So, there would be a distinct GDI+ bitmap information class that GDI+ could work with, but there would not be any more memory overhead related to the bits for an extra bitmap. There would also not be a performance reduction, however minor, associated with copying bits over to a copied bitmap section with each drawing call. I added a method called CreateView(), which took a rectangle that specified the bounds of the view:

public EditableBitmap CreateView(Rectangle viewArea)
        throw new ObjectDisposedException("this");
    return new EditableBitmap(this, viewArea);

It delegates the "magic" to a protected constructor:

protected EditableBitmap(EditableBitmap source, Rectangle viewArea)
    stride = source.stride;
        bitmap = new Bitmap(viewArea.Width, viewArea.Height, 
            stride, source.Bitmap.PixelFormat, 

The constructor copies all of the properties from the source bitmap that will be the same. It stores a reference to the bitmap that it is a view on in the Owner property. It then calculates a byte offset from the start of the owner's byte array to the first pixel of the view bitmap. It then creates a GDI+ Bitmap object, passing in the offset byte pointer and the view's width and height. So far, so good! We have a bitmap object that tricks GDI+ into thinking that it is a standalone bitmap, when it really points to part of a larger bitmap.

However, we still have to worry about those pesky resource management issues. The EditableBitmap class depends on a pinned byte array to store the pixel data. With bitmap views, that byte array is shared between multiple bitmaps. We want the views to still be operational when the root bitmap is disposed and definitely vice versa, as well. So, we have to make it so that the bit array is only destroyed when there are no more EditableBitmap instances that use it. To do this, I added a new class, SharedPinnedByteArray, that manages the byte array and keeps a reference count. When the reference count reaches zero or it is finalized, it unpins the byte array.

The end result is a much faster rendering time for large, non-screen-format bitmaps. The demo included demonstrates the speed difference. When you run it, it will ask you to choose a bitmap using a file dialog. Choose a large bitmap. Try scrolling around, especially dragging the scrollbar thumbs. You will most likely see "tearing:" areas that paint slowly enough that you can easily see the missing area. Now click on the bitmap display area so that the title bar of the form says "New Method." Try scrolling again and it should be very smooth.


  • 5 June, 2007 -- Article edited and posted to the main article base
  • 15 November, 2006 -- Original version posted


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

J. Dunlap
Web Developer
United States United States
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

You may also be interested in...

Comments and Discussions

PraiseMy vote of 5 Pin
EzequielKees5-Jul-17 2:00
memberEzequielKees5-Jul-17 2:00 
QuestionUse EditableBitmap in Visual Basic 2010 Pin
h. de boer16-Oct-11 10:50
memberh. de boer16-Oct-11 10:50 
GeneralRotation [modified] Pin
Scalee1-Feb-10 11:45
memberScalee1-Feb-10 11:45 
GeneralRe: Rotation Pin
Scalee2-Feb-10 13:25
memberScalee2-Feb-10 13:25 
GeneralDrawImageUnscaled(Bitmap, Point) Pin
Martijn van Dorp20-Jul-09 7:39
memberMartijn van Dorp20-Jul-09 7:39 
GeneralCloning Pin
swight9-Apr-09 16:20
memberswight9-Apr-09 16:20 
GeneralRe: Cloning Pin
J. Dunlap16-Apr-09 9:50
memberJ. Dunlap16-Apr-09 9:50 
GeneralRe: Cloning Pin
swight19-Apr-09 15:45
memberswight19-Apr-09 15:45 
GeneralBig thanks! Pin
Froggr8-Apr-09 6:15
memberFroggr8-Apr-09 6:15 
Questionmain image and regions of interest Pin
alleyes26-Feb-09 3:32
professionalalleyes26-Feb-09 3:32 
AnswerRe: main image and regions of interest Pin
Froggr8-Apr-09 6:00
memberFroggr8-Apr-09 6:00 
GeneralRe: main image and regions of interest Pin
alleyes8-Apr-09 6:04
professionalalleyes8-Apr-09 6:04 
GeneralUsed your EditableBitmap class in my codeproject Pin
Berend Engelbrecht30-Jun-08 0:34
memberBerend Engelbrecht30-Jun-08 0:34 
QuestionRe: Used your EditableBitmap class in my codeproject Pin
alleyes26-Feb-09 4:01
professionalalleyes26-Feb-09 4:01 
AnswerRe: Used your EditableBitmap class in my codeproject Pin
Berend Engelbrecht26-Feb-09 7:59
memberBerend Engelbrecht26-Feb-09 7:59 
QuestionRe: Used your EditableBitmap class in my codeproject Pin
alleyes26-Feb-09 9:07
professionalalleyes26-Feb-09 9:07 
AnswerRe: Used your EditableBitmap class in my codeproject Pin
Berend Engelbrecht26-Feb-09 10:07
memberBerend Engelbrecht26-Feb-09 10:07 
GeneralRe: Used your EditableBitmap class in my codeproject Pin
alleyes26-Feb-09 10:27
professionalalleyes26-Feb-09 10:27 
Generalexercises in c# Pin
Mr.PoorEnglish9-Jan-08 11:40
memberMr.PoorEnglish9-Jan-08 11:40 
GeneralGood work, but... Pin
Adam Byrne16-Nov-06 6:36
memberAdam Byrne16-Nov-06 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 5 Jun 2007
Article Copyright 2006 by J. Dunlap
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid