0

I'm trying to add noise to my image and show it in a pixturebox,then blur it and show it in another picturebox too. But i see two blurred image on my pictureboxes. How can i show both of them? Note: I don't want create new Bitmap.

Filtreler f1 = new Filtreler();  
Bitmap Orj = new Bitmap(pBox_SOURCE.Image);
f1.Imge = Orj;

if (SablonBoyutu % 2 == 1)
{
   f1.addnoise(f1.Imge);
   pictureBoxNoisyImg.Image = f1.Imge;
   f1.meanfilter(SablonBoyutu, f1.Imge);
   pBox_PROCESSED.Image = f1.Imge;
}
class Filtreler
{
   private Bitmap resim;   
   public Bitmap Imge
   {
      get { return resim;  }
      set { resim = value; }
   }

 .... (my filters)
}

2 Answers2

0

I think you need one more copy (img2) of your image

f1.addnoise(f1.Imge);
pictureBoxNoisyImg.Image = f1.Imge;
var img2 = new Bitmap(pictureBoxNoisyImg.Image);
f1.meanfilter(SablonBoyutu, img2);
pBox_PROCESSED.Image = img2;

Or

f1.addnoise(f1.Imge);
pictureBoxNoisyImg.Image = new Bitmap(f1.Imge);
f1.meanfilter(SablonBoyutu, f1.Imge);
pBox_PROCESSED.Image = f1.Imge;

Edit

To dispose old images you can do

f1.addnoise(f1.Imge);
if(pictureBoxNoisyImg.Image != null)
{
    pictureBoxNoisyImg.Image.Dispose();
    pictureBoxNoisyImg.Image = null;
}
pictureBoxNoisyImg.Image = new Bitmap(f1.Imge);
f1.meanfilter(SablonBoyutu, f1.Imge);
if(pBox_PROCESSED.Image != null)
{
    pBox_PROCESSED.Image.Dispose();
    pBox_PROCESSED.Image = null;
}
pBox_PROCESSED.Image = f1.Imge;
DotNet Developer
  • 2,973
  • 1
  • 15
  • 24
  • Note to OP : Creating `Bitmaps` carelessly will cause you *GDI Our of Memory issues* sooner or later, all `Bitmaps` need to be disposed. So special care should be paid to how they are created and when they are disposed. – TheGeneral Jul 23 '19 at 05:58
  • Thank you, it works. But i'm looking for a way to write it without create more bitmaps. Is there a way? – Tyler Miles Jul 23 '19 at 06:12
  • You said no other way. But it works with Clone() method. I'm confused. – Tyler Miles Jul 23 '19 at 07:06
  • I said because your original code (using `new` keyword) is fine. All you have to do is to dispose old images before setting new ones. See my Edit – DotNet Developer Jul 23 '19 at 07:10
  • Last question, is it possible do more than one filtering using only one Bitmap? – Tyler Miles Jul 23 '19 at 08:54
  • You can apply as many processing as you want on one Bitmap, one after the other. But to see the result of each operation you have explained in your question you have to save it into new Bitmap. – DotNet Developer Jul 23 '19 at 09:05
0

There is an alternative method which called Cloning (image.Clone();) instead of using new Bitmap Instance. Maybe it will help to you. What's the difference between Bitmap.Clone() and new Bitmap(Bitmap)?