« Careful using Mouse.OverrideCursor | Main | User Experience Review »
Monday
Dec072009

Creating an Image from PixelShaders in Silverlight and WPF

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (2)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments (39)

Australia's magnificent transformation Cheap Ugg Boots is not a brand concept, but collectively snow boots, so, UGG points are many brands. Currently the world's hottest Cheap UGG Jimmy Choo, called "UGG Australia", it is the United States Deckers company's brand. In fact, first brought to the United States is not the Deckers UGG company. Ugg Baby Boots early ShaneSteadman invention after a few years, an Australian surfer took snow boots to the United States and established UGGHolding company. Later, he sold the shares of Deckers company. Until the 1990s, at the same time with the UGG patents and UGG Holding shares, the U.S. company Deckers officially launched "Kids UGG Boots Australia" brand snow boots, which swept the globe cheapuggjimmychoo.com. This pair has been the fashion to those comments, "the history of the ugliest shoes", this will create the world's most irresistible pop culture in recent years. Deckers company's brand Jimmy Choo Ugg Australia, even based on more than 50% for several consecutive years of sales growth has become a fashion myth. Today, UGG Jimmy Choo, the inventor will each receive U.S. ShaneSteadman Deckers company sent three pairs of new Cheap UGG Boots Men. Away from the bustle of the mall after the battle, ShaneSteadman later life dedicated to beloved surfing career cheapuggjimmychoo.com. Perhaps many people feel sorry for him, but who would think, then 锟� 10,000 bought UGG Women Boots, even immortal achievements of the hundreds of millions of fashion legend! Guess you have guessed, the story's hero is one of ShaneSteadman UGG Jimmy Choo Boots be the inventor of it? It is. But then, you might think, he must be a multi-millionaire, right? Then you guess wrong. The ShaneSteadman aged 69 still living in "surfing town," the three-bedroom cottage, there is no huge amount of property, there is no Dream Car luxury. And he still have to work 12 hours a day, but not do snow boots, but every year for his small workshop to do 500 hand surfboard cheapuggjimmychoo.com..
October 7, 2011 | Unregistered CommenterAsics Onitsuka
It appears tomy study andlifehave undergonesignificant changes,it is like mygood friend, has becomean importantpart, andit makesmy lifemore colorful.
October 10, 2011 | Unregistered Commentercoach online outlet
It makes our lives more convenient,but also let more people become increasingly dependent,it is my idea for beautiful.
October 10, 2011 | Unregistered Commenterburberry outlet
Very nice site,In future i ready to hear more from you.
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
October 10, 2011 | Unregistered Commenterbelstaff outlet
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 unc
October 13, 2011 | Unregistered Commentergucci handbags on sale
e 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:
October 13, 2011 | Unregistered Commentergucci bags uk
Christian thought their ancestors were http://www.monclerbransondoudoune.fr/ not wearing clothes, lives in the garden of Eden with Adam and Eve naked; there is no discipline bondage live nice and comfortable. But then the snake lured, ate http://www.monclerbransondoudoune.fr/doudoune-moncler-femme-c-4.html the fruit, it began to have Passion, you begin to have a sense of shame, shame unworthy to join together garments of fig leaves, talk to http://www.monclerbransondoudoune.fr/doudoune-moncler-homme-c-5.html hush up a scandal; about human ancestor of clothes is one of the people of the together emotion of shame, there is no shelter for function to resist cold, or perhaps the garden of Eden there would be no cold.
October 19, 2011 | Unregistered Commenterdoudoune moncler
The information you provide must for some people is very important, I am also very interested in this content, thank you! I’ve always been concerned about your article, the content of this course, I may have different views, but the information you provide is very helpful for me, I always support you, yes, I have been doing.
October 22, 2011 | Unregistered Commenterbelstaff
They will have the money to remove any moral conscience, to meet their money be insatiably avaricious desire. A great post. Keep update! I really like your article content
October 22, 2011 | Unregistered Commentercoach online outlet
I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you have been doing this for a while now, how long have you been blogging for?
October 22, 2011 | Unregistered Commenterburberry outlet
eller i bedste fald bruge tyve eller tredive pounds[ til pop ned til salonen og få vores hår vasket og stylet korrekt.
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/
October 31, 2011 | Unregistered Commenterghd glattejern
Every product of this brand has been designed under great control and proficiency, which is why they are becoming so fashionable between the communities. louis vuitton outlet While talking about ladies fashion, http://www.superlouisvuitton.com/ there are various louis vuitton handbags also available and they are well known for its different styles and outlines. louis vuitton outlet Ladies are really passionate to acquire these handbags, http://www.superlouisvuitton.com/Louis-Vuitton-Wallets/ so that they can use them long time in different occasions. http://www.superlouisvuitton.com/Louis-Vuitton-Dog-Bags/Dog-Carrier-50-Softsided-Luggage-Bags-LV.html It has at all times been gathering up the necessities and values of the majority of people.bags put together your life more easy and fashionable. louis vuitton outlet You don't need to worry about your fashion and style sense while you are carrying your n bags in any party. http://www.superlouisvuitton.com/Louis-Vuitton-luggage-bags/ Your bag will keep you always in the of attraction of the peoples.
November 2, 2011 | Unregistered Commenterlouis vuitton outlet
Lequel de tri dans la conception que vous êtes comme la meilleure? Combinaison de couleurs audacieuses pourraient être les facteurs de base dans le style de chemin. http://www.monclerdoudoune-shop.com/ moncler doudoune certainement que vous devez dépenser la conscience dans le sens d'un caractère de maquillage et coiffure sauvage. au sein de votre actualité si vous êtes mise sur les hommes DIM moncler colorant de la manière dont la réduction de compléter veste est extrêmement important. Les manteaux sombres moncler colorants sont également pourrait être l'objectif. moncler doudoune A cette époque le style d'énormes facteurs de style qui consistent en des bracelets, http://www.monclerdoudoune-shop.com/Moncler-Doudoune-Enfant-FR/ des gants de moto peut être considéré comme un élément crucial cruciale pour la modélisation. http://www.monclerdoudoune-shop.com/Doudoune-Moncler-Femme/ Ne vous souhaitez pour obtenir la fille séduisante? moncler doudoune La coloration rouge-couleur de réduire vestes soulève la popularité. http://www.monclerdoudoune-shop.com/Doudoune-Moncler-Homme/ Cela rend la veste réduisant peut aussi être rempli de charme. Les courts de conception montrera vos courbes plan de bénéfique.
November 2, 2011 | Unregistered Commentermoncler doudoune
Den verdenskjente ghd rettetang, kjent for sin ekstremt jevn, flat og høy teknologi. ghd rettetang er det beste stedet for stjerner og starlets, og det samme nå, kan du også bruke den til å skape den stilen du ønsker. http://www.rettetangbillig.com/ GHD rettetang som den beste hair straightener mark. Den ghd flatt jern og varmen raskt og opprettholde temperaturen. Mai er den mest passende temperatur er nådd, http://www.ghdconz.com/ som er tidligere enn de andre produktene i motsetning til andre lignende produkter Business er hot på lokalt nivå, ghd rettetang motsetning til andre varer, som ikke har blitt fastslått hot spot. Mens andre likeretter produkter hår.
November 2, 2011 | Unregistered CommenterGHD rettetang
It makes our lives more convenient,but also let more people become increasingly dependent,it is my idea for beautiful.
November 9, 2011 | Unregistered Commentermichael kors bags
Very nice site,In future i ready to hear more from you.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
November 9, 2011 | Unregistered Commentertory burch flats sale
It makes our lives more convenient,but also let more people become increasingly dependent,it is my idea for beautiful.
November 9, 2011 | Unregistered CommenterMoncler outlet
"I quite agree with you.All right. I agree with you.I really disagree with you. Women should enjoy the same rights as men. Why can't a woman have her own career?Personally, I do not agree with this viewIf the opinion is right, they are deprived of the opportunity of exchanging error for truth;First, ask questions. Asking question doesn't mean you disagree with the statement. It simply means you're curious. You want to learn more.
"
November 15, 2011 | Unregistered CommenterNorth Face Clearance

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.