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. User Control

User Control

Scheduled Pinned Locked Moved C#
helpcsharpcomquestion
4 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.
  • B Offline
    B Offline
    bubuzzz
    wrote on last edited by
    #1

    Hi, i am making a small image viewer application by using C#. I created a user control which will take the image input and draw it by using the override method OnPaint, and then add the control into the flowlayoutpanel. In FlowLayoutPanel, i set the autoscroll = true. Everything works fine, except when i scroll down the panel, the images become vague and the program has to repaint them. Please look at the screenshot for more details http://i979.photobucket.com/albums/ae280/jasonvoorheeszzz/bug.png[^] Have anyone got the suggestion to fix it?

    L 1 Reply Last reply
    0
    • B bubuzzz

      Hi, i am making a small image viewer application by using C#. I created a user control which will take the image input and draw it by using the override method OnPaint, and then add the control into the flowlayoutpanel. In FlowLayoutPanel, i set the autoscroll = true. Everything works fine, except when i scroll down the panel, the images become vague and the program has to repaint them. Please look at the screenshot for more details http://i979.photobucket.com/albums/ae280/jasonvoorheeszzz/bug.png[^] Have anyone got the suggestion to fix it?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      maybe you are running out of memory somehow. are those basically large pictures drawn at a very small scale? if so, you should create smaller versions, and use those to do your painting. we'll need more information, some numbers and some code to offer actual help. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      B 1 Reply Last reply
      0
      • L Luc Pattyn

        maybe you are running out of memory somehow. are those basically large pictures drawn at a very small scale? if so, you should create smaller versions, and use those to do your painting. we'll need more information, some numbers and some code to offer actual help. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        B Offline
        B Offline
        bubuzzz
        wrote on last edited by
        #3

        Thank you for replying me. I dont think this is the problem of the memory usage coz i checked it carefully. This is the code my ImageArea class (user control):

        public Image LoadImage()
        {
        Image tmpImage = Image.FromFile(imageName);
        Bitmap tmpBitmap = new Bitmap(100, 100);
        Graphics graphics = Graphics.FromImage((Image)tmpBitmap);
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.DrawImage(tmpImage, 0, 0, 100, 100);

                tmpImage.Dispose();
                graphics.Dispose();
        
                return (Image)tmpBitmap;
            }
        
            protected override void  OnPaint(PaintEventArgs e)
            {
                Image tmpImage = LoadImage ();
                Graphics graphics = e.Graphics;
                graphics.DrawImage(tmpImage, 0, 0);
                tmpImage.Dispose();
                graphics.Dispose();
            }
        

        When i press the button ok in the openFileDialog, it will run the AddImageArea function to add the control to the flowLayoutPanel And this is the code of AddImageArea

        public ImageArea AddImageArea(string filename)
        {
        ImageArea pbObj = new ImageArea();

                pbObj.ImageName = filename;
                pbObj.Width = 100;
                pbObj.Height = 100;
                pbObj.ImageName = filename;
                pbObj.BorderStyle = BorderStyle.FixedSingle;
                
                return pbObj;
            }
        
        L 1 Reply Last reply
        0
        • B bubuzzz

          Thank you for replying me. I dont think this is the problem of the memory usage coz i checked it carefully. This is the code my ImageArea class (user control):

          public Image LoadImage()
          {
          Image tmpImage = Image.FromFile(imageName);
          Bitmap tmpBitmap = new Bitmap(100, 100);
          Graphics graphics = Graphics.FromImage((Image)tmpBitmap);
          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
          graphics.DrawImage(tmpImage, 0, 0, 100, 100);

                  tmpImage.Dispose();
                  graphics.Dispose();
          
                  return (Image)tmpBitmap;
              }
          
              protected override void  OnPaint(PaintEventArgs e)
              {
                  Image tmpImage = LoadImage ();
                  Graphics graphics = e.Graphics;
                  graphics.DrawImage(tmpImage, 0, 0);
                  tmpImage.Dispose();
                  graphics.Dispose();
              }
          

          When i press the button ok in the openFileDialog, it will run the AddImageArea function to add the control to the flowLayoutPanel And this is the code of AddImageArea

          public ImageArea AddImageArea(string filename)
          {
          ImageArea pbObj = new ImageArea();

                  pbObj.ImageName = filename;
                  pbObj.Width = 100;
                  pbObj.Height = 100;
                  pbObj.ImageName = filename;
                  pbObj.BorderStyle = BorderStyle.FixedSingle;
                  
                  return pbObj;
              }
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          bubuzzz wrote:

          graphics.Dispose();

          that is wrong. You should dispose of those objects that offer a Dispose() method if you created them (by calling new, or a method with "Create" in the name). you should not dispose of objects others created, such as the Graphics you borrowed from PaintEventArgs.

          bubuzzz wrote:

          return (Image)tmpBitmap;

          is OK, however IMO you don't need the cast. One bad thing is you are loading and resizing all those images inside OnPaint(); OnPaint() should be fast (most of the time), so you would do better by keeping the small images in memory, maybe in a Dictionary (filename, smallImage), which you could load beforehand, or when necessary, but not every time the system decides it has to (re)paint. Warning: Dictionaries tend to grow all the time, depending on the nature of your app, you may want to take care of that. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          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