Applying Filtering to images

Sep 27, 2010 at 7:37 PM

Hi,

I have image on my Silverlight page. What I need is to apply filters to that image and then convert it into writeablebitmap object for saving into database via wcf.

Would you be able to show some sample code of how it can be achieved with .NET Image Tools (saving into database via wcf already exists).

Thanks!

Sep 28, 2010 at 5:04 PM

Hello malignate88,

Could you please assist with that? Also, I have referenced ImageTools.Utils in my Silverlight 4.0 application but for some reason I can not instantiate objects from that library. I really need contrast and brightness features in my project. Your assistance would be much appreciated.

THANKS!

Coordinator
Sep 30, 2010 at 9:07 AM

Hi gettir,

unfortunalty the wiki entry is empty. Did you download the source code and did you have a look to the sample? This would be very helpful.

It is very simple:

Add a reference to ImageTool.Filtering assembly and the using:


using ImageTools.Filtering;

Then instanciate the filter.

Brigtness brightness = new Brightness(100);

Image.Apply(myImage, brightness);

Sep 30, 2010 at 4:28 PM

Hello malignate88,

Thank you for your answer.

Yes, I did try that. The problem I face is that application of the filter should be done against Image object of the .NET Image Tools, not regular Silverlight Image object. I do have binary, or stream or Image of Silverlight at my disposal to work with in XAML's code behind. The problem though is to transform binary (or stream) into that .NET Image Tools object. I did try that but the instance I am getting is always empty.

Is it a bug or may be I do something incorrectly? Would you be able to get me a snippet of how to instantiate that object and populate it from the binary or stream and then, after filter is applied, transform back to binary? I would appreciate if you could assist. I would rather use your library in my project than anything else.

Thanks!

Coordinator
Sep 30, 2010 at 5:02 PM
Edited Sep 30, 2010 at 5:04 PM

Okay, I try to give you a full understanding of image tools:

There are three tasks:
(1) Load an image
(2) Manipulate the image by applying a filter.
(3) Save the image.

STEP 1: Load the image.
You have first to register the decoders that you want to support:
http://imagetools.codeplex.com/wikipage?title=Working%20with%20different%20image%20formats&referringTitle=Home

ImageTools.IO.Decoders.AddDecoder<PngDecoder>();
ImageTools.IO.Decoders.AddDecoder<JpegDecoder>();

Lets say we only want to support png and jpeg at the moment.

Then we have to load or generate the image. There are a lot of different ways:
(a) From a stream:
Image myImage = new Image();
myImage.SetSource(myStream);

(b) From an uri, but recognize that the image must be accessible from silverlight.
http://msdn.microsoft.com/en-us/library/cc645032%28VS.95%29.aspx
Image myImage = new Image();
myImage.UriSource = new Uri(...);

(c) From another image:
Image myImage = new Image(otherImage);

(d) From a control by making a screenshot (add reference to
ImageTools.Utils):
Image myImage = myGrid.ToImage();

(e) From a writeablebitmap (add reference to ImageTools.Utils):
Image myImage = myBitmap.ToImage();

(f) Directly use a image decoder. Not recommended.

IMPORT: The image is loaded asynchronously in (a) and (b), so subscribe
the the events:
Image myImage = new Image();
myImage.LoadingCompleted += new EventHandler(image_LoadingCompleted);
myImage.LoadingFailed += new EventHandler(image_LoadingFailed);

STEP 2: Apply the filter.

Like described before:
Image imageWithFilter = Image.Apply(myImage, new Brightness());

STEP 3: Write the image to a stream:

When we want to write the image to a stream, we also need the Utils library:

Create a stream:

using (Stream stream = ... )
{
myImage.WriteToStream(stream);
}

Thats it, I hope it helps. As a summary:

public void LoadImage(Stream stream)
{
Image image = new Image();
image.LoadingCompleted += new EventHandler(image_LoadingCompleted);
image.SetSource(stream);
}

private void image_LoadingCompleted(object sender, EventArgs e)
{
Image image = sender as Image;

Image imageWithChangedBrightness = Image.Apply(new Brightness(100));
using (Stream stream)
{
imageWithChangedBrithness.WriteToStream(stream)
}
}


When you want and need more help, contact me by msn:
malignate@hotmail.de...I also happy when somebody wants to help
extending the engine.

Sep 30, 2010 at 6:35 PM

Thank you for sample code. I will try to apply it in my project. But before I do could you please confirm that it is fully functional for Silverlight 4.0?

Thanks!

Coordinator
Sep 30, 2010 at 6:39 PM
Confirmed
Mar 18, 2014 at 4:14 AM
Recently, I approach many image converting programs during I am developing my document image reading, viewing, processing and saving library. If you need help for the second step-converting the image in the Silverlight page into writeablebitmap, I can help you by sharing some image converting. Contact with me.