p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Visual Basic 2010 Programmer's Reference (http://p2p.wrox.com/book-visual-basic-2010-programmers-reference-551/)
-   -   Resizing a Bitmap Image (http://p2p.wrox.com/book-visual-basic-2010-programmers-reference/86226-resizing-bitmap-image.html)

Gary108 January 3rd, 2012 07:41 PM

Resizing a Bitmap Image
 
Hi Rod,

Is there an easy way change the size of a Bitmap image? For example, I may have an image that is 800 pixels wide and 1200 pixels high and I want to change it to 640 pixels wide and 900 pixels high?


Also, the same image which would be a single color like black on white, I would like to change the darker color to RGB 256,0,0,0. The white background could remain the same.


I tried looking in VB Helper but for some reason it is not letting me search.

As always, I really appreciate your help.


Regards,

Gary

Rod Stephens January 4th, 2012 11:31 AM

Yes, Google decided to not allow searches from the site any more, won't tell me why, and won't fix it. You can still search the site in any normal Google search box by entering "site:vb-helper.com" at the end as in:

Code:

resize bitmap .net site:vb-helper.com
Then you can find this example:

Resize an image in VB .NET

and this one:

Use antialiasing to resize images smoothly in VB.NET

Gary108 January 7th, 2012 11:29 AM

Private Sub btnScale_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnScale.Click
' Get the scale factor.
Dim scale_factor As Single = Single.Parse(txtScale.Text)

' Get the source bitmap.
Dim bm_source As New Bitmap(picSource.Image)

' Make a bitmap for the result.

Dear Rob,
Thank you for your reply. I am still having some trouble with the following code. What does "Clnt" refer to? It seems that it would refer to the name or a bitmap but so far I cannot figure how to use it? I downloaded the example but when I tried to open the individual files so I could find an answer, it is prompting me to run some kind of conversion software. Any help you can give me, I will really appreciate.

Regards,


Gary




Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))

' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)

' Display the result.
picDest.Image = bm_dest
End Sub

Rod Stephens January 7th, 2012 11:42 AM

CInt converts a number to an integer. So in this code, it takes the floating point result of scaling the bitmap's width and height and converts them to integers because you can only make bitmaps with integer heights and widths.

You're probably using a newer version of Visual Basic than the one I used to write that example. If you open the .sln file, it should upgrade it to your version. It should only take a minute or so.

Gary108 January 8th, 2012 01:15 PM

Pixel Resize
 
Hi Rod,

Yes. Thank you for your explanation. Once you explained that CInt converts a number into an integer I understood my mistake. I had actually entered Clnt. With that correction it works perfectly as it was designed to do. Still now I see that what I need is a little different.

In practice, the original image would likely be a scanned image 6.4 inches wide and 9 inches high, which would be proportionate to the pixel sizes or the width and Height of the new image. What I really need is way to convert any image so that its final size would be exactly 640 pixels wide and 900 pixels High.

Is there another function that can automatically convert any image to a specific pixel size no matter what the original size is? Since the original image will be proportionate to the final image there should be no issue of the image being distorted.

I look forward to your reply. Thank you for your ongoing help.

Regards,


Gary

Rod Stephens January 8th, 2012 04:41 PM

Try something like this:

Code:

Dim bm_dest As New Bitmap(640, 900)

' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, 640, 900)

' Display the result.
picDest.Image = bm_dest

I think that should work.

Gary108 January 10th, 2012 07:38 PM

Hi Rob,

In the following code, what does variable bm_source refer to? I assume that it is the name of the image that we are trying to resize to a 640 pixels by 900 pixels. If that is true, does it include the path, and in what format is it in?



Dim bm_dest As New Bitmap(640, 900)

'Make a Graphics object for the result Bitmap
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

'Copy the source image into the destination bitmap
gr_dest.DrawImage(bm_source, 0, 0, 640, 900)

'Display the result.
picShowPicture.Image = bm_dest


Also, can you tell me how I can fill that variable (bm_source) using the OpenFileDialog box? The code I am presently using to select my image is:


Private Sub btnSelectPicture_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

'Show the Open Dialog Box.
If ofdSelectPicture.ShowDialog = DialogResult.OK Then

'Load the picture into the picture box.
picShowPicture.Image = Image.FromFile(ofdSelectPicture.FileName)

'Show the name of the file in the form's caption.
Me.Text = "Picture View(" & ofdSelectPicture.FileName & ")"

End If




As always I really appreciate your help. I couldn't do this without you.


Regards,


Gary

Rod Stephens January 10th, 2012 07:58 PM

Hi Gary,

bm_source is a Bitmap holding the image you want to resize. I think you could load it from a file as in:

Code:

Dim bm_source As New Bitmap(filename)
Where filename is the file's name. It could be an absolute path including the drive letter and everything, or it could be just the file's name if it's in the same directory as the executing program.

You could use a dialog box something like this:

Code:

' Show the Open Dialog Box.
If ofdSelectPicture.ShowDialog() = DialogResult.OK Then
    ' Load the picture into the bitmap.
    Dim bm_source As New Bitmap(ofdSelectPicture.FileName)

    ' Resize the bitmap.
    ...

    ' Display the result.
    picResized.Image = bm_dest
End If



All times are GMT -4. The time now is 03:13 PM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.