CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2008
    Posts
    12

    Stretchblt in .NET

    Hi im new to .net 2010 and im having a problem with using stretchblt.

    Im trying to load in a BMP , copy part of that image and paste it back in the same image but stretched out. Then display and save the result.

    First i pieced some code together that grabbed an image from a picture box , coppied part of it and pasted it back into the image. This worked fine except that it also copied anythingthat was sat on top of the image in the picture box as well as the image data.

    Code:
     
    Private Sub but_Render_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_Render.Click
    
            pbx_main.Image = CType(RenderImage(pbx_main), Bitmap).Clone
        End Sub
    
        Private Function RenderImage(ByVal src As PictureBox) As Bitmap
    
            'Create bitmap from image file
            Dim srcBmp As New Bitmap(LoadedImageFileName)
    
            'Create a Graphics object in memory from that bitmap
            Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
    
            'Get a Graphics Object from picture box
            Dim srcPic As Graphics = src.CreateGraphics
    
            'get the IntPtr's of the graphics
            Dim HDC1 As IntPtr = srcPic.GetHdc
    
            'get the IntPtr's of the graphics
            Dim HDC2 As IntPtr = srcMem.GetHdc
    
            'StretchBlt the picture 
            StretchBlt(HDC2, 200, 200, 200, 200, HDC1, 50, 50, 50, 50, TernaryRasterOperations.SRCCOPY)
    
            'Clone the bitmap so we can dispose this one 
            RenderImage = srcBmp.Clone()
    
            'Clean Up 
            srcPic.ReleaseHdc(HDC1)
            srcMem.ReleaseHdc(HDC2)
            srcPic.Dispose()
            srcMem.Dispose()
            srcBmp.Dispose()
    
        End Function
    So ive been trying to load in the bitmap from a file rather than take the one displayed in the picture box but nothing seems to work.
    Last edited by vitaloverdose; September 29th, 2010 at 02:31 PM. Reason: better explained

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Stretchblt in .NET

    Can't do that with BMP. Use JPG or PNG
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2008
    Posts
    12

    Re: Stretchblt in .NET

    Ive tried doing something like

    Code:
       
        Private Sub but_Render_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_Render.Click
    
            pbx_main.Image = CType(RenderImage(pbx_main), Bitmap).Clone
     end sub
    
     Private Function RenderImage(ByVal src As PictureBox) As Bitmap
    
            'Create bitmap from image file
            Dim srcBmp As New Bitmap(LoadedImageFileName)
            Dim DestBmp As New Bitmap(LoadedImageFileName)
    
            'Create a Graphics object in memory from that bitmap
            Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
            Dim DestMem As Graphics = Graphics.FromImage(DestBmp)
    
            'get the IntPtr's of the graphics
            Dim HDC1 As IntPtr = srcMem.GetHdc
            Dim HDC2 As IntPtr = DestMem.GetHdc
    
            'StretchBlt the picture 
            StretchBlt(HDC2, 200, 200, 200, 200, HDC1, 50, 50, 50, 50, TernaryRasterOperations.SRCCOPY)
    
            'Clone the bitmap so we can dispose this one 
            RenderImage = DestBmp.Clone()
    
            'Clean Up 
            DestMem.ReleaseHdc(HDC1)
            srcMem.ReleaseHdc(HDC2)
            DestMem.Dispose()
            srcMem.Dispose()
            srcBmp.Dispose()
    
        End Function
    But nothing happens.

  4. #4
    Join Date
    Feb 2008
    Posts
    12

    Re: Stretchblt in .NET

    What cant i do with a Bitmap dglienna?

    Ive tried changing the line

    Code:
    Dim srcPic As Graphics = src.CreateGraphics
    to something else but nothing seems to work.

  5. #5
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Stretchblt in .NET

    Or you can just use the Graphics.DrawImage method. It will stretch the image for you.

    Code:
            Dim destinationImage As New Bitmap(100, 100)
            Dim sourceImage As Image = PictureBox1.Image
            Dim g As Graphics
    
            g = Graphics.FromImage(destinationImage)
    
            g.DrawImage(sourceImage, New Rectangle(0, 0, destinationImage.Width, destinationImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel)
    
            g.Dispose()
    
            PictureBox2.Image = destinationImage
    Last edited by Craig Gemmill; September 30th, 2010 at 08:52 AM.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Stretchblt in .NET

    Quote Originally Posted by vitaloverdose View Post
    What cant i do with a Bitmap dglienna?

    Ive tried changing the line

    Code:
    Dim srcPic As Graphics = src.CreateGraphics
    to something else but nothing seems to work.
    http://en.wikipedia.org/wiki/Lossless_compression
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Feb 2008
    Posts
    12

    Thumbs up Re: Stretchblt in .NET

    Thanks Craig, i was able to produce the effect i was after from the code example you provided.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured