Create a bitmap from another bitmap

Posted on 2006-03-20
Last Modified: 2010-04-23

What's the easiest way to create a bitmap object that contains an offset rectangle within an existing bitmap?  I thought I had a (long) method using BitBlt, but turns out if didn't work...

Question by:riceman0
    LVL 85

    Accepted Solution

    Like this?...

            Dim sourceBmp As Bitmap = Image.FromFile("c:\someImage.bmp")
            Dim sourceRect As New Rectangle(100, 50, 75, 25)

            Dim destBmp As New Bitmap(sourceRect.Width, sourceRect.Height)
            Dim g As Graphics = Graphics.FromImage(destBmp)
            g.DrawImage(sourceBmp, New Rectangle(0, 0, destBmp.Width, destBmp.Height), sourceRect, GraphicsUnit.Pixel)

            ' do something with destBmp...
            PictureBox1.Image = destBmp


    Author Comment


    You rock.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Just FYI, when you load an image using Image.FromFile(), the actual image file remains LOCKED until you dispose of the Bitmap class (or Image class) that opened it.

    So "c:\someImage.bmp" will be locked until "sourceBmp" is explicitly disposed of or goes out of scope.

    You can load an image without locking the file by first using Image.FromFile(), and then creating another Bitmap instance from the first Bitmap instance and then Disposing of the first Bitmap:

        Dim bmpA As Bitmap = Image.FromFile("c:\someImage.bmp") ' file is locked
        Dim bmpB As Bitmap = New Bitmap(bmpA) ' create a copy in memory
        bmpA.Dispose() ' file is unlocked now

    Now you can work with bmpB and "c:\someImage.bmp" will NOT be locked because we disposed of bmpA.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now