Results 1 to 6 of 6

Thread: resize an image

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    resize an image

    Hi! I'd like to know if it is possible to resize an image, loaded at runtime, to fit the picturebox dimension but keeping the proportions. But I don't want to copy it resized... I have an images in a folder, that I load to 3 to 4 different place in the application, the picturebox not being the same size everywhere, I need to only resize it, not make a resized copy of the image... is that clear enough, do you understand what I mean!???
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: resize an image

    Hi,

    Not directly, if I understand you correctly. You can use the stretchmode on a picturebox, but then the picture will lose proportions. I reckon the best way to do it would be to put a second picturebox inside the first to hold the picture and compare the ratio of the sides of your outer "container" picturebox (fixed size) to your desired picture (could be anything). From that, you can figure out whether to set the inner picturebox to be the same as the width of (and hence of smaller height than) the outer picturebox, or to set it to be the same height as (and hence smaller width than) the outer picturebox. Then set the size of the inner picturebox and stretch your picture to fit that.

    Does that make sense?

    zaza

  3. #3
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: resize an image

    Just draw the image yourself using a Graphics object in the paint event of a picturebox or a panel or whatever. Calculating the new dimensions is not hard.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  4. #4

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: resize an image

    ok, I have this. It works fine with small size picture. But when I try with a full hi res picture @5mo, (what the application I'm designing is intended to) it crashes running out ot memory...

    VB Code:
    1. Public Sub ResizeToRatio(ByRef p As PictureBox)
    2.         Dim i As Image = p.Image
    3.         Dim b As Bitmap
    4.         Dim h, w As Double
    5.         Dim ratio As Double = i.Width / i.Height
    6.         '
    7.         h = i.Height
    8.         w = i.Width
    9.         '
    10.         b = New Bitmap(i, w, h)
    11.         '
    12.         While ((b.Width > p.Width) Or (b.Height > p.Height))
    13.             w -= 1.0 * ratio
    14.             h -= 1.0
    15.             b = New Bitmap(i, w, h)
    16.         End While
    17.         '
    18.         p.Image = b
    19.         p.SizeMode = PictureBoxSizeMode.CenterImage
    20.     End Sub
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  5. #5
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: resize an image

    I imagine it happens here:
    VB Code:
    1. While ((b.Width > p.Width) Or (b.Height > p.Height))
    2.             w -= 1.0 * ratio
    3.             h -= 1.0
    4.             b = New Bitmap(i, w, h)
    5.         End While
    where you are creating a new bitmap numerous times.

    You should just make sure that you have that image in memory, and then paint it over the picturebox when its Paint event fires.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  6. #6

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: resize an image

    ok, well in memory, you mean, a memorystream!?

    its a path + filename I retreive from a dataset, so I shlould put it in a memory stream and then paint the resize... I'm sorry, I'm really not good with GDI+....
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

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