|
-
September 29th, 2010, 12:07 PM
#1
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
-
September 29th, 2010, 02:53 PM
#2
Re: Stretchblt in .NET
Can't do that with BMP. Use JPG or PNG
-
September 29th, 2010, 03:45 PM
#3
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.
-
September 30th, 2010, 12:12 AM
#4
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.
-
September 30th, 2010, 08:33 AM
#5
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.
-
September 30th, 2010, 11:37 AM
#6
Re: Stretchblt in .NET
 Originally Posted by vitaloverdose
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
-
September 30th, 2010, 02:47 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|