JSP*_*JSP 14 wpf image-processing imagesource
我的问题是图像加载似乎与应用程序资源不正确.这是代码:
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,,/WpfApplication3;component/Resources/Images/16x16_incorrect.png", UriKind.Absolute);
bi.EndInit();
ImageSource s = bi;
Run Code Online (Sandbox Code Playgroud)
图像文件"16x16_incorrect.png"是16x16 32bpp PNG文件,但在执行上面的代码后,s.Width = s.Height = 21,59729 ....我还有另一个文件 - "16x16_correct.png",当我加载它以相同的方式,ImageSource的宽度和高度都等于16,002.
我有一大堆有用的PNG 16x16 32bpp图像,我打算在我的应用程序的UI中使用它.不幸的是,它们每个都加载不正确并且看起来模糊(或平滑),因为系统将它从16x16拉伸到21x21.
您是否愿意如此友好地解释这个问题的可能解决方案?如果源图像文件中存在问题,如何将ImageSource.Width更改为所需大小才能使用此文件?
Dom*_*nic 18
如果您不想在外部更改DPI,可以使用以下方法执行此操作:
public static BitmapSource ConvertBitmapTo96DPI(BitmapImage bitmapImage)
{
double dpi = 96;
int width = bitmapImage.PixelWidth;
int height = bitmapImage.PixelHeight;
int stride = width * bitmapImage.Format.BitsPerPixel;
byte[] pixelData = new byte[stride * height];
bitmapImage.CopyPixels(pixelData, stride, 0);
return BitmapSource.Create(width, height, dpi, dpi, bitmapImage.Format, null, pixelData, stride);
}
Run Code Online (Sandbox Code Playgroud)
如果你只需要Image.Source.Width/Height中的正确值,你可以像我一样做:
this.myImage.Tag = new double[] { bitmapImage.DpiX, bitmapImage.DpiY };
this.myImage.Source = bitmapImage;
Run Code Online (Sandbox Code Playgroud)
并像这样调整大小:
public static void ResizeImage(Image img, double maxWidth, double maxHeight)
{
if (img == null || img.Source == null)
return;
double srcWidth = img.Source.Width;
double srcHeight = img.Source.Height;
// Set your image tag to the sources DPI value for smart resizing if DPI != 96
if (img.Tag != null && img.Tag.GetType() == typeof(double[]))
{
double[] DPI = (double[])img.Tag;
srcWidth = srcWidth / (96 / DPI[0]);
srcHeight = srcHeight / (96 / DPI[1]);
}
double resizedWidth = srcWidth;
double resizedHeight = srcHeight;
double aspect = srcWidth / srcHeight;
if (resizedWidth > maxWidth)
{
resizedWidth = maxWidth;
resizedHeight = resizedWidth / aspect;
}
if (resizedHeight > maxHeight)
{
aspect = resizedWidth / resizedHeight;
resizedHeight = maxHeight;
resizedWidth = resizedHeight * aspect;
}
img.Width = resizedWidth;
img.Height = resizedHeight;
}
Run Code Online (Sandbox Code Playgroud)
Nir*_*Nir 11
您需要将图像分辨率设置为96 DPI(目前错误的png为71.12).
你可以使用免费的paint.net程序(http://getpaint.net)从图像菜单中选择画布大小并将"分辨率"字段设置为96
这是因为图像的DPI.WPF以96 dpi呈现默认值.如果你看一下不正确的png图像的dpi.您将看到它设置为72.这会导致WPF将图像缩放到96 DPI并保持原始大小.
有两种解决方案.您可以:
将"宽度"和"高度"属性设置为16,将"拉伸"属性设置为"统一"
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image x:Name="MyIncorrectImageFixed" Source="http://i.piccy.info/i5/24/41/504124/16x16_incorrect.png" Width="16" Height="16" Stretch="Uniform" />
<Image x:Name="MyIncorrectImage" Source="http://i.piccy.info/i5/24/41/504124/16x16_incorrect.png" Stretch="None" Grid.Row="1" />
<Image x:Name="MyCorrectImage" Source="http://i.piccy.info/i5/22/41/504122/16x16_correct.png" Stretch="None" Grid.Row="2" />
Run Code Online (Sandbox Code Playgroud)