Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Drawing a big image to real coordinates

Drawing a big image to real coordinates

Scheduled Pinned Locked Moved C#
graphicshelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ianhunt01
    wrote on last edited by
    #1

    Hi, I have got a big topographical jpg file that I want to display in a form on a picturebox in its real coordinates that ranges X from 50 000 to 75 000 and Y from 2 800 000 to 2 825 000. this will make it very easy for me to get the mouse coords in real coords as the user invokes the Mousedown event. It will also make it easier for me to draw real features in its real coordinates. I have got the code:

    Image image = Image.FromFile(@"C:\Images\2529DA.jpg");
    Pen myPen = new Pen(Color.Black);
    Graphics formGraphics = this.picGrid.CreateGraphics();
    formGraphics.Clear(picGrid.BackColor);

    formGraphics.DrawImage(image, 0, 0, 1000, 1000);

    This shows the topomap nicely on the screen but with the local picturebox coords from 0 to 1000. If I change the code to

    formGraphics.DrawImage(image, 50000, 2800000, 25000, 25000);

    This previous code then puts the map WAY offscreen to the south-east ! I tried fiddling with the following code but no luck.

    formGraphics.DrawImageUnscaled(image,0, 0);
    formGraphics.DrawImageUnscaledAndClipped(image, rect1);

    please help ! cheers Ian

    O 1 Reply Last reply
    0
    • I ianhunt01

      Hi, I have got a big topographical jpg file that I want to display in a form on a picturebox in its real coordinates that ranges X from 50 000 to 75 000 and Y from 2 800 000 to 2 825 000. this will make it very easy for me to get the mouse coords in real coords as the user invokes the Mousedown event. It will also make it easier for me to draw real features in its real coordinates. I have got the code:

      Image image = Image.FromFile(@"C:\Images\2529DA.jpg");
      Pen myPen = new Pen(Color.Black);
      Graphics formGraphics = this.picGrid.CreateGraphics();
      formGraphics.Clear(picGrid.BackColor);

      formGraphics.DrawImage(image, 0, 0, 1000, 1000);

      This shows the topomap nicely on the screen but with the local picturebox coords from 0 to 1000. If I change the code to

      formGraphics.DrawImage(image, 50000, 2800000, 25000, 25000);

      This previous code then puts the map WAY offscreen to the south-east ! I tried fiddling with the following code but no luck.

      formGraphics.DrawImageUnscaled(image,0, 0);
      formGraphics.DrawImageUnscaledAndClipped(image, rect1);

      please help ! cheers Ian

      O Offline
      O Offline
      oobimoo
      wrote on last edited by
      #2

      You want to fit a 25000x25000 bitmap in a 1000x1000 bitmap without losing precision? :wtf:

      I 1 Reply Last reply
      0
      • O oobimoo

        You want to fit a 25000x25000 bitmap in a 1000x1000 bitmap without losing precision? :wtf:

        I Offline
        I Offline
        ianhunt01
        wrote on last edited by
        #3

        Yes, (I mean yes, I want to loose the precision in order to know where he is) I need to get the user to select an area he wants to zoom into with the mouse. I want to get the e.X and e.Y of the beginning and end of his zoom and redraw the image meaning i have to move and scale the image with this new info. I checked later and saw that the image has a resolution of 6500 x 6500 although it covers and area of 25 kilometers by 25 kilometers. Does this help ? Ian

        O 1 Reply Last reply
        0
        • I ianhunt01

          Yes, (I mean yes, I want to loose the precision in order to know where he is) I need to get the user to select an area he wants to zoom into with the mouse. I want to get the e.X and e.Y of the beginning and end of his zoom and redraw the image meaning i have to move and scale the image with this new info. I checked later and saw that the image has a resolution of 6500 x 6500 although it covers and area of 25 kilometers by 25 kilometers. Does this help ? Ian

          O Offline
          O Offline
          oobimoo
          wrote on last edited by
          #4

          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();
              }
          
          I 1 Reply Last reply
          0
          • O oobimoo

            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();
                }
            
            I Offline
            I Offline
            ianhunt01
            wrote on last edited by
            #5

            Awesome, Thanks a lot - the whole matrix issue was a bit daunting but it makes more sense now. I will dig in right now and try it out. cheers Ian

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups