off-line viewer of smaller map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mirian Ryskea
    New Member
    • Dec 2010
    • 3

    off-line viewer of smaller map

    Morning,

    I need make app in C# which can show map of area about 10sq miles. But map must be in bigger scale and I need to find some way how to scroll it.

    First I try very primitive way - load map in pictureBox and move whole component. But with big image (gif, about 10-20 MB) it flashes while moving (redrawing) the map.

    So I tried to - first clone, then draw - image in "static" pictureBox. It was good, but it gradually consumed RAM and then made out of memory error. I

    Can you give me a tip what can I try? Thank you very much... (and excuse my english O:))

    M.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You should be able to load the image in memory, then draw a crop of it, fitting that to your main window. Then you can scroll the crop window around inside the main image, drawing the appropriate section.

    The DrawImage method on a graphics object should have a way for you to draw a cropped image, have a look at the methods available on it and if you have troubles, let me know. Hopefully that will improve your performance a bit.

    Comment

    • Mirian Ryskea
      New Member
      • Dec 2010
      • 3

      #3
      I tried before to do it like you mentioned in your first paragraph. I use as inspiration this code:

      http://www.dreamincode.net/code/snippet1987.htm (without disposing "BMP")

      ...and I use it to redraw different parts of image while "moving" on the map. It was working (and working as I need), but program gradually took more and more RAM and crashed with out of memory exception.

      I thought it happened becose I have to dispose the "bmp" variable (am I right?). But it can not be done, cose it is return value. And at this point I stucked :(

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        It depends on how you did it... the DrawImage method shouldn't create new memory and you only need to load the source image once. Maybe that's the problem... you don't need that Crop method exactly as it is; in fact, you don't need that at all.

        You probably want to do something like this...

        Code:
        public class MainClass
        {
          private Bitmap m_sourceImage;
          private Rectangle m_cropRect;
        
          public MainClass()
          {
            m_sourceImage = Image.FromFile(...);
            ...
          }
        
          public SomeAppropriateType UpdateCrop(SomeOtherAppropriateType)
          {
            // update m_cropRect as appropriate
          }
        
          public DoMyDraw(Graphics g)
          {
            ...
            g.DrawImage(m_sourceImage, new Rectangle(0, 0, m_cropRect.Width, m_cropRect.Height), m_cropRect, GraphicsUnit.Pixel);
            ...
          }
        }
        The code may not be 100% correct, but that's the general idea. You should only have to load the source image one time. If you were to do the method that you got from the link in a loop, you'd be reloading the image over and over, which would eat up memory (until a garbage collection occurs) and be slow.

        Give this a try and let me know how it works :)

        Comment

        • Mirian Ryskea
          New Member
          • Dec 2010
          • 3

          #5
          I am using picture box to display map. Part, I was missing, was using my graphics at picture box:

          Code:
          g = pictureBox.CreateGraphics();
          ...solved :) thank you very much for you help!

          Comment

          Working...