Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

crop and center an image in form

mydelphimydelphi Posts: 62Member
Hi,

I have a rather difficult question to ask. Here goes.

1, I have a form, buttons, scrollbox and image control.
2, I put an scrollbox in it, and centered (by hand) the
scrollbox.
3, then I put an image inside the scrollbox, and center
it (by hand)

The reason I use the scrollbox is becuase I can center
the it (I want to do this) inside the form, because the
form can stretch depending on the size of the image.
And, because the scrollbox has feature for stopping the
flicker (doublebufrer)

I will be doing several things:
1, adding more controls, like trackbars and other controls.
2, my goal is to be able to size the image with these
trackbars (and also ie, using textbox [320]x[240]) too.
3, In addition to sizing, I also want to be able to CROP
the image (left/right, and top/bottom) independantly, and
have a newly resize and/or CROPPED image, all inside my
scrollbox, inside my form.

I'm having problems with all the above, but the most diffcult
part is the cropping and centering INSIDE my form.

What is the formula for:
1, centering my scrollbox INSIDE my form?
2, I'm using the BITBLT() to push image inside my scrollbox.
do I need to do anything special with the two as well?
example, the scrollbox and image.

Basically, I want to learn how to PROPERLY:
1, size an image
2, crop an image
3, center image inside a form

Sorry, I need to have the image control and the scrollbox,
not one or the other.

Thank you for (anyone) for removing my headacke.
- mydelphi

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi,
    :
    : I have a rather difficult question to ask. Here goes.
    :
    : 1, I have a form, buttons, scrollbox and image control.
    : 2, I put an scrollbox in it, and centered (by hand) the
    : scrollbox.
    : 3, then I put an image inside the scrollbox, and center
    : it (by hand)
    :
    : The reason I use the scrollbox is becuase I can center
    : the it (I want to do this) inside the form, because the
    : form can stretch depending on the size of the image.
    : And, because the scrollbox has feature for stopping the
    : flicker (doublebufrer)
    :
    : I will be doing several things:
    : 1, adding more controls, like trackbars and other controls.
    : 2, my goal is to be able to size the image with these
    : trackbars (and also ie, using textbox [320]x[240]) too.
    : 3, In addition to sizing, I also want to be able to CROP
    : the image (left/right, and top/bottom) independantly, and
    : have a newly resize and/or CROPPED image, all inside my
    : scrollbox, inside my form.
    :
    : I'm having problems with all the above, but the most diffcult
    : part is the cropping and centering INSIDE my form.
    :
    : What is the formula for:
    : 1, centering my scrollbox INSIDE my form?
    : 2, I'm using the BITBLT() to push image inside my scrollbox.
    : do I need to do anything special with the two as well?
    : example, the scrollbox and image.
    :
    : Basically, I want to learn how to PROPERLY:
    : 1, size an image
    : 2, crop an image
    : 3, center image inside a form
    :
    : Sorry, I need to have the image control and the scrollbox,
    : not one or the other.
    :
    : Thank you for (anyone) for removing my headacke.
    : - mydelphi
    :
    Centering a control on a parent (ie scrollbox on a form, image on scrollbox) is quite simple. Just calculate the left property based on the parent.width and the control.width.
    [code]
    ScrollBox1.Left := (ClientWidth-ScrollBox1.Width) div 2;
    [/code]
    Same goes for a vertical center.

    It is possible to copy any rectangular region of an image onto another image. For this you'll need two images: source and dest. To copy a part of the source onto the dest, you'll need to call the dest.canvas.CopyRect() method, and supply the source canvas, and the region to copy. This can easily be used to crop an image in any way you like, just select the right rectangle.

    Sizing an image is almost the same procedure as cropping it, but the call the TCanvas.StretchDraw() method.
  • mydelphimydelphi Posts: 62Member
    Thanks zibadian, I am looking into it now. If I have any other
    questions, I hope you (and others) will still be around to answer
    them.

    Thanks again.
    - mydelphi


    : : Hi,
    : :
    : : I have a rather difficult question to ask. Here goes.
    : :
    : : 1, I have a form, buttons, scrollbox and image control.
    : : 2, I put an scrollbox in it, and centered (by hand) the
    : : scrollbox.
    : : 3, then I put an image inside the scrollbox, and center
    : : it (by hand)
    : :
    : : The reason I use the scrollbox is becuase I can center
    : : the it (I want to do this) inside the form, because the
    : : form can stretch depending on the size of the image.
    : : And, because the scrollbox has feature for stopping the
    : : flicker (doublebufrer)
    : :
    : : I will be doing several things:
    : : 1, adding more controls, like trackbars and other controls.
    : : 2, my goal is to be able to size the image with these
    : : trackbars (and also ie, using textbox [320]x[240]) too.
    : : 3, In addition to sizing, I also want to be able to CROP
    : : the image (left/right, and top/bottom) independantly, and
    : : have a newly resize and/or CROPPED image, all inside my
    : : scrollbox, inside my form.
    : :
    : : I'm having problems with all the above, but the most diffcult
    : : part is the cropping and centering INSIDE my form.
    : :
    : : What is the formula for:
    : : 1, centering my scrollbox INSIDE my form?
    : : 2, I'm using the BITBLT() to push image inside my scrollbox.
    : : do I need to do anything special with the two as well?
    : : example, the scrollbox and image.
    : :
    : : Basically, I want to learn how to PROPERLY:
    : : 1, size an image
    : : 2, crop an image
    : : 3, center image inside a form
    : :
    : : Sorry, I need to have the image control and the scrollbox,
    : : not one or the other.
    : :
    : : Thank you for (anyone) for removing my headacke.
    : : - mydelphi
    : :
    : Centering a control on a parent (ie scrollbox on a form, image on scrollbox) is quite simple. Just calculate the left property based on the parent.width and the control.width.
    : [code]
    : ScrollBox1.Left := (ClientWidth-ScrollBox1.Width) div 2;
    : [/code]
    : Same goes for a vertical center.
    :
    : It is possible to copy any rectangular region of an image onto another image. For this you'll need two images: source and dest. To copy a part of the source onto the dest, you'll need to call the dest.canvas.CopyRect() method, and supply the source canvas, and the region to copy. This can easily be used to crop an image in any way you like, just select the right rectangle.
    :
    : Sizing an image is almost the same procedure as cropping it, but the call the TCanvas.StretchDraw() method.
    :

  • mydelphimydelphi Posts: 62Member
    Hi zibadian,

    After much fussing around, I managed to get it to work. I had
    other controls inside the form, and that would completly throw
    off the centering. This drove me crazy, cause I didn't know to
    watch out for this (when you have other controls inside) and
    those different sizes had to be added into the equation some how.
    I guess, next time, you'll remember this and factor this in when
    you help others out again.

    But, it works, and I'm happy.
    Thank you for all you help, again.
    - mydelphi
  • mydelphimydelphi Posts: 62Member
    Hello PH people.

    This is part 2 of 2.

    When last I posted, I said I was happy. I am, but have a little
    bit of issue. But, I won't go into it at this time, cause it's
    working out ok. On with part 2.

    CROPPING and SIZING
    Ok, I have been messing around with trying to crop my image,
    using all kinds of methods, but none that is working accurately,
    or at all.

    Here's what I have so far:
    1, I open my app
    2, it displays the form, scrollbox inside form, and timage inside
    the scrollbox.
    3, I can open an image (timage) and line 4 is welcomed.
    4, I can size the tform1 window, and the complete image with the
    scrollbox centers perfectly. i also put some code inside the
    tform's OnResize to keep the scrollbox and timage centered - works
    every time.

    Great for the above!

    Now, I want to CROP the image at: the top, the left edge and right
    edge and bottom edge. I want to be able to crop any one of them at
    will. Even if I only want to crop just the top or just the right or
    just the left or just the bottom.

    1, CROP - I'll be using 4 trackbars for top/left/right/bottom.
    2, RESIZE - I'll be using 2 trackbars for this.

    When I display the timage insize the scrollbox, and if I decide to
    crop or resize the image, I want my scrollbox to resize as well, BUT,
    I want to leave a little footprint (a boarder, like maybe 4 pixels)
    so that I know exactly where timage boundery is. At current, I have
    my scrollbox's background color set to BLUE, so it's easy to see the
    boarders or boundery, but this is the issue I stated at the top which
    I'm still having trouble with. But, lets just assume that it's
    actually working as planned here.

    1, I would love to have my 4 trackbars be able to crop the image,
    giving my image a new image crop (or size, but without actually be
    resized)
    2, I would love to have my 2 trackbars RESIZE my image

    After messing around this this all day (since my last post to you guys)
    I'm really exhausted. And, I haven't ben able to get it to work. I'd
    really like your assisatance here - please.

    Can someone provide the necessary code to properly crop an image and
    also some code to resize an image.
    Again, I am using the BITBLT() function that actually displays the
    complete image. I basically use LOADFILE(filename) for the picture
    (timage)

    This is the routine I am using (I need this routine):
    [code]
    BitBlt(
    im.Canvas.Handle,
    0,0, // coord. for dest ?
    pture.Width,
    pture.Height,
    pture.canvas.handle, // loaded from file
    0,0, // coord. from source ?
    SRCCOPY
    );

    im.Repaint;
    [/code]

    I would really appreciate it. Well, I'm going to go walk (1 mile) to
    the store and get some coffee, and refresh my mind from this madness,
    arrr! I'll be back in an -/+ hour.

    Thank you all for your help.
    - mydelphi
Sign In or Register to comment.