User Control
-
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?
-
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?
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).
-
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).
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; }
-
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; }
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).