我有一个透明的PNG,将其转换为ImageSource时会失去很多质量。我要做的转换如下:
public static ImageSource ToImageSource()
{ Bitmap bitmap = Properties.Resources.Image;
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
RenderOptions.SetBitmapScalingMode(wpfBitmap, BitmapScalingMode.HighQuality);
return wpfBitmap;
}
但是质量真的很差。当我直接在计算机上调用文件时,质量是正确的:
<DataGridTemplateColumn Width="14" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="C:\Users\MyUser\Desktop\Image.png" Width="14" Height="14"></Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
还有另一种方法可以在不损失质量和透明度的情况下转换资源吗?
答案 0 :(得分:0)
遗憾的是,我无法测试您的代码,但我认为您必须将模式设置为NearestNeighbor
尝试
public static ImageSource ToImageSource()
{
Bitmap bitmap = Properties.Resources.Image;
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
RenderOptions.SetBitmapScalingMode(wpfBitmap, BitmapScalingMode.NearestNeighbor);
return wpfBitmap;
}
答案 1 :(得分:0)
或者...试试这个:)
break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end
调用public static ImageSource ToImageSource()
{
Bitmap bitmap = Properties.Resources.Image;
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
new Int32Rect(0, 0, bitmap.Width, bitmap.Height),
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
return wpfBitmap;
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
将释放GDI句柄。
答案 2 :(得分:-1)
尝试一下:
BitmapImage image = new BitmapImage(new Uri("your image path here", UriKind.Relative));
编辑:假设您具有图像路径。