Learn how to a build a cloud-first strategyRegister Now


Get a rectangle from a bitmap and save to file

Posted on 2005-04-08
Medium Priority
Last Modified: 2006-11-18

I want to select a part of a bitmap (for example rectangle from 40,40 to 100,100 and save this as new bitmap to a file...

Is this possible?


Question by:Dumdidum
  • 2
LVL 12

Expert Comment

ID: 13739899
Well you could use BitBlt....  I'll see if there is a VB.NET equiv'
LVL 12

Expert Comment

ID: 13739939
   Const SRCCOPY As Integer = &HCC0020
    Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer

    Dim BitmapSrc As New Bitmap("C:\X.bmp")
    Dim BitmapSrcGraphic As Graphics
    Dim BitmapSrcHDC As IntPtr
    BitmapSrcGraphic = BitmapSrcGraphic.FromImage(BitmapSrcObj)

    Dim BitmapDest As New Bitmap(320, 240)
    Dim BitmapDestGraphic As Graphics
    Dim BitmapDestHDC As IntPtr
    BitmapDestGraphic = BitmapDestGraphic.FromImage(BitmapDestObj)

    Dim BltReturn As Integer

    BltReturn = BitBlt(BitmapDestHDC.ToInt32, 0, 0, 320, 240, BitmapSrcHDC.ToInt32, 5, 5, SRCCOPY)


Non tested... let me know of problems
LVL 32

Accepted Solution

Erick37 earned 120 total points
ID: 13739989
Maybe this:

        'Source bitmap
        Dim img As Bitmap
        img = Bitmap.FromFile("c:\test.jpg")
        'Target bitmap
        Dim bmp As New Bitmap(60, 60, img.PixelFormat)
        Dim g As Graphics = Graphics.FromImage(bmp)

        'Areas to draw to/from
        Dim dstRect As New Rectangle(0, 0, 60, 60)
        Dim srcRect As New Rectangle(40, 40, 100, 100)
        'Draw the areas and save
        g.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel)
        bmp.Save("c:\test2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        'clean up

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 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