Image enlargement
I think everyone should have used ACDSEE or Windows XP, they all support the magnification and roaming of images, although there is related information on the Internet, but they are not very comprehensive. Today I will introduce a way, due to the relationship of the space, mainly How to achieve, try to make a conciseness, well, we start now. Description: 1. This article uses Object Pascal to describe, please convert the reader to the related code, the author is not related to the code 2. Try to use the Windows API and bit operation
Theoretical: Keywords: Drawing Zone - the area of the window display image, can also be a full screen (good to the full screen drawing is better than the normal window) center point - ie the center point to draw the zone display in the original image Coordinate (declaration: This concept is especially important)
Let's talk about the image amplification, to enlarge a picture, our general practice is to zoom in image directly, but this article introduces only the part we can see, enlarge the difference, one is to zoom in compared with the drawing area It is also small, this situation is nothing to say, of course, it shows all images; the second is that the zoomed image is larger than the drawing area, which is the key topic we have to discuss today. In this case, we must first determine the image. The size of the amplified, then calculate the position and size of the original image according to the "central point", and finally enlarge the intercepted image to the plot area. Tell the image roaming, when the displayed image exceeds the drawing area, we need to roam on the image to see all images. The principle is: When the mouse is clicked in the plot area, start roaming, first record the click position of the mouse, then detect the movement of the mouse, calculate the "central point" according to the mouse and the last displacement (you need the screen coordinates Convert to the original image coordinate), according to the principle amplified above to the original image, the portion to be displayed is removed, and the display is displayed to the plot area.
Algorithm implementation articles: 1. Image amplification variable definition: PZOOM: Amplification (integer: 100%, 100%, 100% or more), but not recommended to use floating point numbers) A, B: Central Point W, H: To intercept the width and high x, y: The position to intercept SW, SH: the width and high P1, P2 of the original image: Zoom ratio AW, AH: large size of the image after the image PW , PH: Patter size Vx, Vy: Position (left upper corner) VW, VH: size PTX, PTY: Temporary variables displayed in the plot area
Known variables: PZOOM, (A, B), (SW, SH), (P1, P2), (AW, AH), (PW, pH) variables to be calculated: (x, y), (W, h), (vx, vy), (VW, VH) start calculation: AW = ROM * SW / 100); AH = ROM * SH / 100); P1 = AW / PWP2 = AH / PH // Note: Round is used to get together, such as int (), fix (), etc. IF P1> 1 THEN W = Round (SW / P1) Else W = SWIF P2> 1 THEN H = ROUND (sh / p2) Else H = sh // Note: SHR is the right movement operator, you can use ">> 1", "DIV 2", "/ 2" or "Round (W / 2)" instead of x = AW shr 1Y = BH SHR 1
// Note: DIV is the entire operator PTX = (w * pzoom) Div 100Pty = (h * pzoom) Div 100
// The following calculation image size and position variables displayed in the drawing area Pencent: double; // Zoom ratio wx: double; // Wide-shrinkage hX: double; // high score // get zoom ratio wx: = PW / PTX HX: = PH / PTY IF WX> HX THEN PENCENT: = HX Else Pencent: = WX;
// Get the final size VW: = Round (Pencent * PTX); VH: = Round (Pencent * Pty);
/ / Calculate the location of the picture vx: = (pW-VW) Div 2; Vy: = (pH-VH) DIV 2; // ----------------- -----------------
Ok, two important tasks are completed (x, y), (w, h), (vx, vh), all calculated, the following work is displayed, we choose Windows API Operation / / The following display image --------------------- Variable SDC is the original picture device handle (DC) TDC is a temporary device handle DDC final device handle
Bitblt (TDC, 0, 0, W, H, SDC, 0, 0, Srcopy); SetStretchbltMode (DDC, Stretch_Deletescans); Stretchblt (DDC, 0, 0, VW, VH, TDC, 0, 0, W, H, Srccopy);
Finally, draw the area to the display: for example: bitblt (getdc (0), VX, VY, VX VW, XY VH, DDC, 0, 0, SRCCopy; // --------- ---------------------------
2. Image roam first define three global variables: fbegindragpoint: tpoint; // Record the position of the mouse started to drag the position fbegindragsbpoint: tpoint; // Remove the "central point" position fbegindrag: boolean; // has started "Drag" A , B: integer; // "center point" position
When the mouse is left mock, record the position of the mouse and the "central point" position, and when setting fbegindrag, set fbegindrag to move to the mouse to move, if the fbegindrag is moved, if the fake is not processed, if For the truth: assuming x, y is the current position of the mouse A = fbegindragpoint.x - (x-fbegindragpoint.x) * 100) Div pzoom = fbegindragpoint.y - (Y-fbegindragpoint.y) * 100) Div pzoom
Finally, the image is used to enlarge the image.
Skills: 1. If the image is large, the bitmap object using Delphi will have an internal assault error. At this time, you can set the following: BitImage: = tbitmap.create; bitiMage.pixelformat: = Pf24bit; BitImage.ReleaseHandle; 2. If To make the image automatically adapt to the size of the window, refer to the following code: VAR P1, P2: Double; Begin P1: = PW / SW; P2: = PZW; if P1> P2 THEN PZOOM: = ROUND (p2 * 100) Else PZOOM: = ROUND (p1 * 100); if pzoom = 0 THEN PZOOM: = 100; END; 3. To make the image have good effect, it is best to make this method in full screen mode for "Crazy Marker", if Readers are interested to see the effect Ling Li software studio http://www.wosens.net2003.12.7