Render WriteableBitmap in WPF - wpf

I have noticed a bizzare situation - in SIlverlight, when a picture is rendered using WriteableBitmap Render method, the resulting pic is very sharp. Of course in WPF Render method is not available. I have used DrawingGroup to render two images on top of each other (I have got a png with transparency and standard JPEG). However in that case the resulting bitmap is not so sharp (especially in case of text).
Could you see any reasons for that? Have you faced such a problem?
Any ideas for the solution?
I need to 'put' a png with transparency on jpeg image and get a precise resulting pic.
Thank you in advance for the replies!
Cheers

Have you tried BitmapScalingMode on RenderOptions? It's an attached property that applies to most DependencyObjects that have anything to do with drawing images. An example:
<object Name="myObject" RenderOptions.BitmapScalingMode="HighQuality" .../>
or to do this in codebehind:
RenderOptions.SetBitmapScalingMode(myObject, BitmapScalingMode.HighQuality);

Related

WPF Image Color Tint

I am loading several images in WPF and from what I have read, it keeps them all in memory.
I was wondering why it is that when I display my image, that there is a red/purple tint?
Has anyone experienced this issue before?
I set up an Image class using the Designer, then I set the Image.Source to my ImageSourceConverter.ConvertFromString("MyFilepath.png")
I also set
Image.Stretch = Stretch.Fill (if that helps)
It seems that some images have a reddish discoloration to them. I cannot figure out why...?
EDIT:
I tried to post images, but stackoverflow's convenient spam prevention said I could not post them because I just joined...
Is it possible there's a translucent overlay in your XAML causing the discoloration? It could also be an encoding issue... Can you post a sample of your code?
Also, although you can't embed images directly in your post because of your newness, you could upload the image to imgur.com, for example, and post a link to that image.
Well, I found that after I changed the images from .png to .jpg, the discoloration was no longer an issue. I am wondering what might have caused this?
I do not think it was the alpha transparency of the .png's, but rather the ImageSourceConverter itself...?
This was most likely caused by dithering effect. It should go away after you set the following property on your Image element to true:
SnapsToDevicePixels = "True"

Make part of an image transparent

I want to put an image on a button, but I want part of the image to be transparent. How do I do this?
Try the Image.OpacityMask property. You can give it a brush that specifies the region you want to be transparent.
EDIT: From MSDN:
There is no direct support for
color-keying a bitmap in WPF.
However, it is fairly easy to
implement on your own. Dwayne has
implemented a ColorKeyBitmap on his
blog:
http://blogs.msdn.com/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
I believe it links to the code on
Codeplex as well. You could also
accomplish this simply by reading your
bitmap into system memory, iterating
through all the pixels and setting
their values yourself, and
constructing a new bitmap out of that
array.
Use a paint program (I use Paint.Net) to change the area you want transparent to an alha=0 color. Then save the image (mine was JPG) as a PNG. Seemed to work fine for me in the WPF Image control.

How can I stretch bitmap in WPF without smoothing pixels

I'm working on SEM image processing application, written in WPF. I have an image display control, derived from Canvas, which displays image & overlays using DrawingVisuals (one for each "layer"). It also implements Zoom & Pan using scale & translate transform, applied on DrawingVisuals.
When I zoom in the image to see individual pixels, they are displayed smooth, evidently using bilinear filtering to stretch the bitmap (no surprise, as WPF is rendered through Direct3D). However, for my use case, I would rather see individual pixels as sharp boxes, as usual in any image editor like Photoshop. That's why user of my app zooms the image -> to be able to operate on pixel level.
Is there such option in WPF (other than manually stretching the bitmap before displaying it)? I was not able to find anything.
thanks in advance,
Zbynek Vrastil
Czech Republic
Finally found an answer, with some help from Experts Exchange. Class RenderOptions defines attached property BitmapScalingMode, which can be set to NearestNeighbor. So,
RenderOptions.SetBitmapScalingMode(imageDisplay, BitmapScalingMode.NearestNeighbor);
does the trick.
Zbynek Vrastil
Hate to put a dampener on things, but if NearestNeighbor works like GDI+, then this will give you a limited success. As you increase magnification in areas of high contrast you might not get the desired results. In GDI+ you find blacks becoming blue, and whites becoming red - again I stress in areas of high contrast! If this isn't the case in WPF, think yourself lucky!
Perhaps a WCF developer could confirm that?
I've found that there are more options to consider, but I can only speak for the GDI+ Graphics class, which might be useful to someone.
Graphics graph = e.Graphics;
graph.InterpolationMode = InterpolationMode.NearestNeighbor;
graph.CompositingQuality = CompositingQuality.AssumeLinear;
graph.SmoothingMode = SmoothingMode.None;
This works for me. I think the SmoothingMode is the trick. Hope this helps someone else out there.

UIElement to PNG - Text slightly blurred

I am using ImageTool's PNG encoder to create an image.
I have a Grid that contains multiple TextBlocks, each TextBlock contains dynamic text.
When I create a WriteableBitmap from the grid containing the TextBlocks, I then use ImageTool's encoder to convert the WriteableBitmap to a PNG image.
All works well, however, when I view the PNG image (am saving the file to the hard drive for testing purposes) - the text looks slightly blurred. Is this an issue with the encoder or the WriteableBitmap class? And - has anyone experienced this before and are there workarounds?
Thanks.
I'm a part of ImageTool project but I'm playing as a tester since I'm using this library in one of my project... If you can create a sample then you can probably show us so that we can test in our machine.
You can also try with Joe Stegman's encoder or fJCore JPEG encoder.

silverlight: reflection effect

I've seen examples in silverlight where the achieve a effect using 1 of 2 ways:
Two image objects, both
displaying the same image, but the
bottom one rotating it 180 degrees
and applying a gradient image
opacity
A MediaElement object and a VideoBrush element.
I have a series of path objects, rectanges, etc which I would like to apply a reflection affect to. Is there a more elegant solution other than copying my objects manually and rotating them? Unfortunately the VideoBrush object only works on MediaElement objects, but is there something else I can use?
Not in Silverlight, in WPF you have a VisualBrush which can help with this but Silverlight doesn't have one in version 2. Just to be clear though, it's not a rotation it's a negative ScaleY on a ScaleTransform. The easiest thing is to put everything into a UserControl and then use two UserControls with one having a ScaleTransform and an OpacityMask on it.
For a good example, please see the following blog post from Mike Snow.
http://silverlight.net/blogs/msnow/archive/2008/09/04/silverlight-tip-of-the-day-36-how-to-create-reflections-and-shadows-for-images-and-text.aspx

Resources