I'm making an windows 8 application.
I've a ScrollViewer with an Image inside.
The source image is really big, and the rendering with the Image is really bad (because the Image is really smaller than the source image, so the scaling make the result ugly).
Here is how I set the source image:
var baseUri = new Uri("ms-appx:///");
var bitmapImage = new BitmapImage(new Uri(baseUri, CurrentLocalization.MapPath));
MapImage.Source = bitmapImage;
My question is : how can I set the interpolation quality of the Image to have a better rendering? I don't find any ScalingMode option or InterpolationQuality...
You would need to use DirectX or SharpDX to achieve such results.
Related
I'm writing a little program that tranfers a screen shot of a user screen to my wpf image control but when the image is displayed it is not full,not the whole screen is displayed even if the action was done through my computer and not my leptop and it is blurry
Bitmap bmp=
new Bitmap((int)SystemParameters.PrimaryScreenWidth(int)SystemParameters.PrimaryScreenHeight);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
the code above is the code to take the screenshot
BitmapImage bmI = new BitmapImage();
bmI.BeginInit();
bmI.UriSource = new Uri(FullPath);
bmI.EndInit();
Screen_Shot.Source = bmI;
And this code is the code to dislpay the image, Screen_Shot is the image name, and the full path is where i put the bitmap image
I tried to use
Screen_Shot.Stretch = Stretch.Fill; and UseLayoutRounding="True" SnapsToDevicePixels="True"
but none of them seems to get the job's done
this is an exsample of a screenshot i took from my own pc
You want to set the Stretch property to Uniform or else your image will be distorted as it will fill regardless of the Image control's size/shape. You also have to remember that the screen size may be different than your programs window size. If you don't uniformly stretch the image it will distort almost always (even if it's just a bit).
Regarding your "blurry" image, you need to specify the rendering option of you Screen_Shot image control or it will set the image to a lower quality that it can render more easily. This is honestly better to just set in the xaml. I normally use Fant, but look other options specified here.
<Image Name="Screen_Shot" RenderOption.BitmapScalingMode="Fant"/>
I have a small UWP application that load same image in two ways:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
var bitmapImage2 = new BitmapImage(new Uri(BaseUri, "TestImage.png"));
_image.Source = bitmapImage1;
//_image.Source = bitmapImage2;
The problem is that Image control shows the same image in different ways. For bitmapImage1 image is not smoothed, but for bitmapImage2 it's ok. How it looks like. I need to do some manipulation with image before show (change some pixels) but I also need to have smoothed image after that. Could you please help me with it?
I also used WriteableBitmap to change some pixels and have same result (not smoothed). Looks like I need to tell to Image control how to draw this.
Here is link to project for more information
I tested your code on my side. And you will found if you don't set the Height and Width properties for the image control, the pictures will looks all the same. So the reason for one looks smooth but another looks not is the image control scaling the picture. Without scaling the picture will not looks different. I guess this may be leaded but difference between vector and bitmap image.
You should be able to resolve it by create a smooth picture with the size you just want to show in the app, and set the image control to the same size with the picture or without setting the Height and Width properties, this may resolved.
My result:
I've got solution on another thread.
If change order of code lines it helps:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
_image.Source = bitmapImage1;
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
So we should assign Image source and than set source stream to Bitmap.
It seems, Image control decodes the picture quite flexibly so that it can suit for acutal UI size. If you set the BitmapImage to Image.Source first, the picture is to be smoothed properly like the second one.
I want to copy an image with all its parameters, including the manipulation data (scale, rotatio etc).
I'm creating a new image control and copy the source and size but can't find a way to set the same manipulation data.
I'm using System.Windows.Controls.Image and i have the ManipulationData object of the source image.
Have any idea?
Thanks
There's very good nuget package available for this..
https://www.nuget.org/packages?q=Tags%3A%22Magick.NET%22
For example to crop or resize an image:
MagickImage image = new MagickImage("filepath.jpg");
image.Crop(new MagickGeometry(424, 448, 224, 224));
image.Resize(123,234);
image.Write("output.jpg");
You can use RenderTargetBitmap to render any UIElement to bitmap:
The class exists in both, WPF and Windows Store Apps, however the usage is slightly different.
WPF Links:
- https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap(v=vs.110).aspx
Windows Store Links:
https://msdn.microsoft.com/library/windows/apps/dn298548
http://social.technet.microsoft.com/wiki/contents/articles/20648.using-the-rendertargetbitmap-in-windows-store-apps-with-xaml-and-c.aspx#Use_its_Pixels
I searched in many forums and I didn't get what I want.
My question is clear and simple : How to resize pixel of WriteableBitmap , for example from 300*300 to 500*500
Look at the WritableBitmapEx project. You can use it or learn how to resize your image.
It is easy to do it by using WritableBitmapEx extensions methods.
// Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation method
var resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);
Image control - placed into Viewbox.
For automatic scaling when the window size changes.
Everything works perfect.
Is it possible to change the type of interpolation in my case?
For example select Bicubic or Bilinear.
Or for automatic scaling of such a choice is not available?
BitmapImage bmp=new BitmapImage(new Uri("c:/temp/1.jpg"));
ImageSource pic = bmp;
Viewbox vb = new Viewbox();
vb.Stretch = Stretch.UniformToFill;
vb.StretchDirection = StretchDirection.DownOnly;
Image img=new Image();
img.Source = pic;
vb.Child = img;
As far as I know you can't specify bicubic or bilinear interpolation for a bitmap in WPF, (the default is linear) but you can set RenderOptions.BitmapScalingMode option to get better control of the display quality of the scaled bitmap. For example:
RenderOptions.BitmapScalingMode="NearestNeighbor" or RenderOptions.BitmapScalingMode="HighQuality"
There's more info on each of the scaling modes on MSDN http://msdn.microsoft.com/en-us/library/system.windows.media.bitmapscalingmode
If you still have issues with blurry graphics try setting UseLayoutRounding="True" in your root element. This will disable sub-pixel positioning of elements that can can cause jagged lines in WPF applications