Get a rectangle from a bitmap and save to file

Posted on 2005-04-08
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
    LVL 12

    Expert Comment

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

    Expert Comment

       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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    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 ( Literal, only instead of starting and ending with w…
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    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