Create a bitmap from another bitmap

Posted on 2006-03-20
Medium Priority
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
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 16237038
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

ID: 16237154

You rock.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16237232
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

850 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