Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 3rd, 2012, 06:41 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question 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
Reply With Quote
  #2 (permalink)  
Old January 4th, 2012, 10:31 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

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
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
The Following User Says Thank You to Rod Stephens For This Useful Post:
Gary108 (January 7th, 2012)
  #3 (permalink)  
Old January 7th, 2012, 10:29 AM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question

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
Reply With Quote
  #4 (permalink)  
Old January 7th, 2012, 10:42 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #5 (permalink)  
Old January 8th, 2012, 12:15 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question 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
Reply With Quote
  #6 (permalink)  
Old January 8th, 2012, 03:41 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #7 (permalink)  
Old January 10th, 2012, 06:38 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question

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
Reply With Quote
  #8 (permalink)  
Old January 10th, 2012, 06:58 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

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
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cloning section of a bitmap image Gary108 BOOK: Visual Basic 2010 Programmer's Reference 1 November 23rd, 2011 10:07 AM
image resizing with CSS LTrout CSS Cascading Style Sheets 2 July 20th, 2006 08:09 AM
New Image (Bitmap) instance or not? jacob C# 2005 0 April 4th, 2006 04:12 AM
Read bitmap image? RichardRose C# 3 October 9th, 2004 11:45 PM



All times are GMT -4. The time now is 05:11 AM.


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