Results 1 to 11 of 11

Thread: change image size

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    change image size

    I have an image that I want to resize, but the property is read-only. I tried creating a bitmap and setting it equal to the image, but that didn't work:
    Code:
                Dim bmp As Bitmap = New Bitmap(x, y)
                bmp = pbBefore.Image
                pbBefore.Image = bmp

    How can I achieve my goal without looping through and drawing each pixel up to a certain length?

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: change image size

    VB Code:
    1. Dim b As New Bitmap(w, h)
    2. Dim g As Graphics = Graphics.FromImage(b)
    3.  
    4. g.DrawImage(pbx.Image)
    5. g.Dispose()
    6.  
    7. pbx.Image = b


    I typed this in i.e, so there may be a few errors.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: change image size

    Thanks. This is what I have now:

    Code:
                        Dim b As New Bitmap(x, y)
                        Dim g As Graphics = Graphics.FromImage(b)
    
                        g.DrawImage(pbAfter.Image, New Point(0, 0))
                        g.Dispose()
    
                        pbAfter.Image = b
    It almost does what I want, but not quit. It increases the drawing surface, which is half of what I want. The actual image doesn't resize. It stays the same and the new area is filled with grey. Any ideas on how to fix that?

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: change image size

    specify the new image size with a rectangle then draw your image in that rectangle
    VB Code:
    1. Dim ImageRect as new Rectangle([I]your rectangle coords[/I])
    2. g.Graphics.DrawImage(pbAfter.Image, ImageRect)
    I think that should do it
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: change image size

    Excellent! That worked perfectly!

    Thanks kevin.

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: change image size

    welcome....
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: change image size

    ahh, kebo, I've got another problem with that now. It's warping the images really bad when I resize them.... Is there some way of getting around this?

  8. #8
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: change image size

    Quote Originally Posted by System_Error
    ahh, kebo, I've got another problem with that now. It's warping the images really bad when I resize them.... Is there some way of getting around this?
    Well, have a look at this:
    VB Code:
    1. Private Sub CreateThumbGenie()
    2.         Dim NewImage As Image = Image.FromFile("filename")
    3.  
    4.         ' Make a bitmap for the result.
    5.         Dim DestSize As New Bitmap( _
    6.         CInt(PictureBox1.Width), _
    7.         CInt(PictureBox1.Height))
    8.  
    9.         ' Make a Graphics object for the result Bitmap.
    10.         Dim GenDest As Graphics = Graphics.FromImage(DestSize)
    11.  
    12.         ' Copy the source image into the destination bitmap.
    13.         GenDest.DrawImage(NewImage, 0, 0, _
    14.         DestSize.Width + 1, _
    15.         DestSize.Height + 1)
    16.  
    17.         ' Display the result.
    18.         PictureBox1.Image = DestSize
    19.  
    20.     End Sub
    21.  
    22. 'later in a command button, you can call it as:
    23. [b]  CreateThumbGenie()[/b]

    This will create a smaller representation of the picture, without distorting

    Hope it helps..
    VB.NET MVP 2008 - Present

  9. #9

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: change image size

    Thanks. I figured out the problem. I was thinking your code was again, causing a distortion, but it's not. I am using a numeric ticker that updates automatically. When it updates automatically I get the distortion. If I enter the value in myself, and then click enter, everything is fine. Is it like, painting over top of each of the images? How can I fix that?


    THanks for the help.

  10. #10
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: change image size

    not really sure....maybe you can post some code?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  11. #11

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: change image size

    Sure...

    This is the handle_resize method which does all the resizing:
    Code:
      If x > 0 And y > 0 Then
                        Dim NewImage As Image = pbAfter.Image
    
                        ' Make a bitmap for the result.
                        Dim DestSize As New Bitmap(x, y)
    
                        ' Make a Graphics object for the result Bitmap.
                        Dim GenDest As Graphics = Graphics.FromImage(DestSize)
    
                        ' Copy the source image into the destination bitmap.
                        GenDest.DrawImage(NewImage, 0, 0, _
                        x, _
                        y)
    
                        ' Display the result.
                        pbAfter.Image = DestSize
                    End If

    Now, it is called EACH time the value of my numeric ticker is changed. The ticker simply calls handle_resize() and does nothing else.


    There's got to be a reason why it's doing this. It's like it's painting overtop of the other image. If I type in the values, then it works fine, but I can't consistently press the ticker.

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