Click here to Skip to main content
15,888,037 members
Home / Discussions / C#
   

C#

 
GeneralRe: Tracing Invalidate calls (Windows Forms) Pin
Jon Hulatt1-Sep-08 2:15
Jon Hulatt1-Sep-08 2:15 
QuestionHow do I draw a single color transparently? Pin
Megidolaon31-Aug-08 22:47
Megidolaon31-Aug-08 22:47 
AnswerRe: How do I draw a single color transparently? Pin
oobimoo31-Aug-08 23:29
oobimoo31-Aug-08 23:29 
GeneralRe: How do I draw a single color transparently? Pin
Megidolaon1-Sep-08 22:56
Megidolaon1-Sep-08 22:56 
QuestionDrawing a big image to real coordinates Pin
ianhunt0131-Aug-08 22:39
ianhunt0131-Aug-08 22:39 
AnswerRe: Drawing a big image to real coordinates Pin
oobimoo1-Sep-08 0:18
oobimoo1-Sep-08 0:18 
GeneralRe: Drawing a big image to real coordinates Pin
ianhunt011-Sep-08 1:03
ianhunt011-Sep-08 1:03 
GeneralRe: Drawing a big image to real coordinates Pin
oobimoo1-Sep-08 8:19
oobimoo1-Sep-08 8:19 
Use the following class
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace some_namespace
{
    class Transformation
    {
        // world: the real world your bitmap represents
        // bitmap: your (unscaled) bitmap
        // screen: the portion of the screen that you display the image (a picture box maybe)

        Matrix matrix_world;       // bitmap to world
        Matrix inv_matrix_world;   // world to bitmap
        Matrix matrix_bitmap;      // screen to bitmap
        Matrix inv_matrix_bitmap;  // bitmap to screen

        Size bitmap_size;
        Size screen_size;


        public Transformation(PointF worldOrigin, SizeF worldSize, 
            Size bitmapSize, Size screenSize)
        {
            matrix_world = new Matrix();
            matrix_bitmap = new Matrix();
            bitmap_size = bitmapSize;
            screen_size = screenSize;

            // if you want the native y-points up coord system for the world, uncomment '-' and
            // '+ worldSize.Height' below
            matrix_world.Scale(worldSize.Width / (float)bitmap_size.Width, 
               /* - */ worldSize.Height / (float)bitmap_size.Height);                 
            matrix_world.Translate(worldOrigin.X, 
                worldOrigin.Y /* + worldSize.Height */, MatrixOrder.Append);
            inv_matrix_world = matrix_world.Clone();
            inv_matrix_world.Invert();
            Reset();
        }
 
        // Reset transformation to the 'screen portion displays entire bitmap'
        public void Reset()
        {
            matrix_bitmap.Reset();
            matrix_bitmap.Scale((float)bitmap_size.Width / (float)screen_size.Width, 
                (float)bitmap_size.Height / (float)screen_size.Height);
            inv_matrix_bitmap = matrix_bitmap.Clone();
            inv_matrix_bitmap.Invert();
        }

        // transform to 'screen portion displays the rectangular portion of the bitmap 
        // defined by the displayOrigin point and the displaySize'
        public void Transform(PointF displayOrigin, SizeF displaySize)
        {
            matrix_bitmap.Reset();
            matrix_bitmap.Scale(displaySize.Width / (float)screen_size.Width, 
                displaySize.Height / (float)screen_size.Height);
            matrix_bitmap.Translate(displayOrigin.X, displayOrigin.Y, MatrixOrder.Append);
            inv_matrix_bitmap = matrix_bitmap.Clone();
            inv_matrix_bitmap.Invert();
        }


        // pretty obvious what they do
        //
        public void ScreenToBitmap(Point[] points)
        {
            matrix_bitmap.TransformPoints(points);
        }

        public void BitmapToScreen(Point[] points)
        {
            inv_matrix_bitmap.TransformPoints(points);
        }

        public void BitmapToWorld(Point[] points)
        {
            matrix_world.TransformPoints(points);
        }

        public void WorldToBitmap(Point[] points)
        {
            inv_matrix_world.TransformPoints(points);
        }

        public void ScreenToWorld(Point[] points)
        {
            ScreenToBitmap(points);
            BitmapToWorld(points);
        }

        public void WorldToScreen(Point[] points)
        {
            WorldToBitmap(points);
            BitmapToScreen(points);
        }
    }
}

Initialize the class by passing the appropriate sizes for your world (25,000x25,000) your bitmap (6,500x6,500) your picture box (1000x1000) and your origin point (a few milions if i remember well from your prevous post). In the initial state you must have your image drawn in the picture box by the graphics.DrawImage(bitmap, 0,0,1000,1000) call. When you want to zoom into a rectangle specified by two points taken from the mouse input (e.g pointMouseDown, pointMouseUp) first you transform these points to the original bitmap coords.
Point[] pts = new Point[]{pointMouseDown,pointMouseUp};
transformation.ScreenToBitmap(pts);
// and drawing
graphics.DrawImage(bitmap,new Rectangle(0,0,1000,1000), pts[0].X,pts[0].Y,pts[1].X-pts[0].X,
pts[1].Y-pts.[0].Y,GraphicUnit.Pixel);
// dont forget to inform transformation object for the zoom
transformation.Transform(pts[0],new Size(pts[1].X-pts[0].X,ts[1].Y-pts.[0].Y);

Whenever you want you can get the world coords of a screen point with the ScreenToWorld method and vice versa.
If you want to zoom out to the initial state just graphics.DrawImage(bitmap, 0,0,1000,1000) and call the Reset method of the transformation object.
\
Regards
GeneralRe: Drawing a big image to real coordinates Pin
ianhunt012-Sep-08 5:49
ianhunt012-Sep-08 5:49 
QuestionFather class how to use the special sub-category type [modified] Pin
huangrongchuan31-Aug-08 21:43
huangrongchuan31-Aug-08 21:43 
GeneralRe: Father class in how to use the special sub-category of type Pin
Guffa1-Sep-08 1:42
Guffa1-Sep-08 1:42 
GeneralRe: Father class in how to use the special sub-category of type Pin
huangrongchuan1-Sep-08 2:29
huangrongchuan1-Sep-08 2:29 
AnswerRe: Father class how to use the special sub-category type Pin
Guffa1-Sep-08 6:38
Guffa1-Sep-08 6:38 
GeneralRe: Father class how to use the special sub-category type [modified] Pin
huangrongchuan1-Sep-08 15:45
huangrongchuan1-Sep-08 15:45 
QuestionHow to transfer all mails from one account to another. Pin
ekpravasi31-Aug-08 20:18
ekpravasi31-Aug-08 20:18 
AnswerRe: How to transfer all mails from one account to another. Pin
Harvey Saayman31-Aug-08 20:41
Harvey Saayman31-Aug-08 20:41 
AnswerRe: How to transfer all mails from one account to another. Pin
ekpravasi31-Aug-08 21:13
ekpravasi31-Aug-08 21:13 
QuestionDevice ID Pin
ellllllllie31-Aug-08 20:16
ellllllllie31-Aug-08 20:16 
AnswerRe: Device ID Pin
Brij31-Aug-08 20:33
mentorBrij31-Aug-08 20:33 
GeneralRe: Device ID Pin
ellllllllie31-Aug-08 23:45
ellllllllie31-Aug-08 23:45 
QuestionHow to get commandline parameter from User Pin
Laji5931-Aug-08 17:35
Laji5931-Aug-08 17:35 
AnswerRe: How to get commandline parameter from User Pin
Vikram A Punathambekar31-Aug-08 18:34
Vikram A Punathambekar31-Aug-08 18:34 
AnswerRe: How to get commandline parameter from User Pin
Brij31-Aug-08 18:35
mentorBrij31-Aug-08 18:35 
AnswerRe: How to get commandline parameter from User Pin
Pete O'Hanlon31-Aug-08 22:50
mvePete O'Hanlon31-Aug-08 22:50 
Question[Message Deleted] Pin
Sunset Towers31-Aug-08 13:47
Sunset Towers31-Aug-08 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.