I have tiff image in wpf application.First i converted the image into bytes using memorystream and then i converted the bytes to bitmapimage. I give bitmapimage to the image.I also take another image and give its source directly image path without conversions.what I observed is The quality of the image after conversions became low. Why it happens?
My Code as follows. I used File.ReadAllBytes("filepath") to convert the image to bytes.
I used below method to get BitmapSource from the byte[].Then I assign the bitmapsource to the image
public static System.Windows.Media.Imaging.BitmapSource ConvertBytesToBitmapSource(byte[] imageByt开发者_JAVA技巧es)
{
System.Drawing.Bitmap source = new System.Drawing.Bitmap(ConvertBytesToImage(imageBytes));
IntPtr imagePtr = source.GetHbitmap();
System.Windows.Media.Imaging.BitmapSource bitmapSour = null;
try
{
bitmapSour = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(imagePtr,
IntPtr.Zero, System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
catch (Exception ex)
{
logger.Error("Error in converting bytes to BitmapSource", ex);
throw ex;
}
finally
{
DeleteObject(imagePtr);
}
return bitmapSour;
}
Thank you Waqas Raja for giving reply.I Solved the Problem while converting the project from 3.5 to 4.0 version,the image quality is decreased.In 3.5 version, by default the image quality is high quality.But in 4.0 version,we need to explicitly specify the quality of the image to get high quality by using the BitScalingMode Dependency property as
RenderOptions.SetBitmapScalingMode(MyImage, BitmapScalingMode.HighQuality);
You can refered the link
http://msdn.microsoft.com/en-us/library/bb613591.aspx
You should use Graphics object to draw the new image instead of just creating the image. here is sample code
System.Drawing.Image sourceImage = System.Drawing.Image.FromStream(sourceStrm, true, true); System.Drawing.Bitmap destBitmap = new System.Drawing.Bitmap(destWidth, destHeight); System.Drawing.Graphics grap = System.Drawing.Graphics.FromImage((System.Drawing.Image)destBitmap); grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; grap.DrawImage(sourceImage, 0, 0, destWidth, destHeight);
if its not the way you used, can you please post some sample code which you used.
精彩评论