tags:

views:

99

answers:

3

I am working with delphi.
I have one scroll box in which I am putting TImage control. Now I wanted to zoom the image rendered into TImage control. So, I am using stretchDraw method of TCanvas. My code is -

   if sbZoom.Down then begin
      rct := imgmain.Picture.Bitmap.Canvas.ClipRect;
      rct := Rect(rct.Left * 2,rct.Top * 2,rct.Right * 2,rct.Bottom * 2);
      imgmain.Picture.Bitmap.Canvas.StretchDraw(rct,imgmain.Picture.Bitmap);
      imgmain.Repaint;
   end;

It is correctly zooming the image, my problem is I want the size of scroll box also should be changed with zooming of image.
Also explain me parameters of Canvas.StretchDraw method. I am little confused with it.
Thank You.

A: 

You should set the size of the image control to the size of the bitmap.

Ritsaert Hornstra
@Ritsaert They are already same...
Himadri
+3  A: 

You can do this quite easily without calling StretchDraw:

  if Zoomed then begin
    Image1.AutoSize := false;
    Image1.Stretch := true;
    Image1.Width := 2*Image1.Width;
    Image1.Height := 2*Image1.Height;
  end
  else begin
    Image1.Stretch := false;
    Image1.AutoSize := true;
  end;

AutoSize := true assures that the TImage is the same size as the picture inside. During zoom we switch AutoSize off and Stretch on, so the picture is resized to the TImage size (which is still the same here). Then we double the size of the TImage to make the zoom effect. As the TImage is now larger, the scrollbox can work properly.

Uwe Raabe
@Uwe But it will not change the value of Image1.Picture.Bitmap.Width and Image1.Picture.Bitmap.Height...
Himadri
It doesn't, but the Scrollbox doesn't care about Image1.Picture.BitmapWidth, it only cares about Image1.Width. Why would you want to resize the actual bitmap when you can show the image larger / smaller without actually destroying information? +1.
Cosmin Prund
@Cosmin I want the same size of bitmap because I have to do further processes with the image on mousedown event and change several points of image.
Himadri
+1  A: 

Uwe Raabe is giving you the right way to do it. Here's why your way doesn't work: A scroll box will show scrollbars and help you see whole controls. In your case, it will only show scrollbars when the TImage object grows larger then the Scrollbox. The Scrollbox can't possibly know the internals of TImage so it doesn't care about TImage.Picture, it only cares about the control. And a TImage that has AutoSize = False doesn't care about it's Picture, it's size stays the same at all times.

Your code repaints the base bitmap onto itself. The problem is, the bitmap has fixed Width and Height: if you paint outside the bitmap's area you're basically silently ignored. When you're "zooming" by StretchDrawing the bitmap onto itself (and I'm surprised it worked to start with!) you're not making the bitmap larger and the stuff that doesn't fit gets silently clipped away. If you do want the internal bitmap to change size then you'll first need to create a new, larger bitmap, draw your enlarged image to the new bitmap and then assign the bitmap to your TImage. If you do this, make sure TImage.AutoSize = True.

Cosmin Prund
@Cosmin OK good answer... Actually I had already tried this but it was also not working as I was expecting. But the problem was the parameters I passed in strechDraw function. I was taking larger bitmap and was passing the same bitmap as a parameter of StrechDraw.. My another question was about parameters of StrechDraw, which nobody explain. Anyway now I had understood It.
Himadri