Creating an Image from PixelShaders in Silverlight and WPF
Monday, December 7, 2009 at 8:00AM While this is not the best way to do image calculations in the code behind in terms of imaging with Silverlight and WPF I was perplexed with the problem of having a great pixel shader that I could apply in the xaml using Bitmap Effects but unclear as the best way to translate that to an actual Bitmap Image. If you are looking for a quick fix solution I wanted to share a little gold nugget in how to actually do this that I found. Just to explain a bit more this assumes you already know how to use Pixel Shaders, I will include a sample code to help illustrate this if you do not. But honestly that is a separate topic. I wish I had the source of this little nugget, it took alot of searching and improvising but eventually I came up with the following code:
In the XAML
<Image x:Name="OriginalImage" Source="img.jpg">
<Image.Effect>
<l:BrightContrastEffect x:Name="MyEffect"
Brightness="{Binding ElementName=bVal, Path=Value}"
Contrast="{Binding ElementName=cVal, Path=Value}"/>
</Image.Effect>
</Image>
In the Code
//using System.Windows.Media.Imaging;
//using System.Windows.Shapes;
//using System.Windows
//using System.Windows.Media;
BitmapSource bitmap = OriginalImage.Source as BitmapSource; //source from XAML
Size sz = new Size(bitmap.PixelWidth, bitmap.PixelHeight);
Rectangle r = new Rectangle();
r.Fill = new ImageBrush(bitmap);
r.Effect = MyEffect; //name applied in XAML
r.Measure(sz);
r.Arrange(new Rect(sz));
var rtb = new RenderTargetBitmap(
bitmap.PixelWidth, bitmap.PixelHeight,
bitmap.Dpi, bitmap.DpiY, PixelFormats.Pbgra32);
rtb.Render(r);
What it essentially does is takes the effect you apply in the XAML and in the back end you can use that effect to create a new bitmap. You do this by virtually constructing a rectangle of the appropriate size and shape of the original image and applying the effect and the image to the rectangle and then saving that out using RenderTargetBitmap. I will upload a sample solution when I get home this afternoon.
Imaging,
Pixel Shaders,
Silverlight 3,
WPF in
Silverlight 3,
WPF 

Reader Comments (39)
i like your post.so In future i ready to hear more from youVery nice site,In future i ready to hear more from you.
i like your post.so In future i ready to hear more from you
Bestemt, hvis vi er på vej på ferieghd glattejern[ eller til en tur i udlandet, og vi ønsker at sikre]at vi ikke ender med at have alt for mange dårlige hår dage, vi forsøger at presse vores hårtørrere, stylingprodukter, kamme, børster, og glattejern ned i kufferten.
http://www.ybyzw.com/
http://www.dkglattejern.com/
http://www.ghdglattejernonline.com/
ghd pink http://www.aubrandshop.com/pink-ghd/
ghd straighteners http://www.aubrandshop.com/
Cheap ghd australia http://www.aubrandshop.com/
ghd australia http://www.australiaghdgift.com/
ghd shop http://www.australiaghdgift.com/ghdstyler/
ghd ireland http://www.ghd-straightener-ireland.com/
GHD Pink Orchid http://www.ghd-straightener-ireland.com/2011-GHD-Pink-Limited/2011-New-Styler-GHD-Pink-Orchid-Limited-Edition.html
Cheap ghd http://ghdhair.sinaapp.com/tag/cheap-ghd/
popular ghd http://ghdstraightener.sinaapp.com/tag/popular-ghd
moncler outlet http://www.ukmoncler.net/
Moncler jackets for men http://www.ukmoncler.net/UK-Moncler-Jackets-outlet/
Moncler kids Jackets http://www.ukmoncler.net/Moncler-Kids-Jackets-uk/
Moncler London http://www.italiamoncler.com/
moncler http://www.italiamoncler.com/
Moncler shop http://www.italiamoncler.com/Moncler-outlet-UK/
Moncler jackets men http://www.italiamoncler.com/UK-Moncler-Jackets-outlet/
moncler Jackets blog http://moncler.sinaapp.com/
Moncler Women jackets http://moncler.sinaapp.com/category/moncler-women-jackets/
"