我想要一些关于我的图像的帮助。我只想保存我的屏幕,很高兴我可以。但有些软件无法正确看到它们。例如:如果我把我的图片(.jpg)放到facebook,或者用Paint打开它,它会是这样的:
但是如果我使用默认的 Windows 程序或某些程序打开相同的图像(完全相同的图像),它将如下所示:
所以,我在我的 WPF 应用程序中做这个“打印屏幕”,这是我的代码:
private void btSalvar_Click_1(object sender, RoutedEventArgs e)
{
BitmapSource bmp = VisualToBitmap.Render(gridSalvar);
Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Imagem JPG(*.jpg)|*.jpg";
if (dialog.ShowDialog() == true)
{
System.Windows.Media.Imaging.PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create);
encoder.Save(fs);
fs.Close();
}
并且为此创建的类:
class VisualToBitmap
{
public static System.Windows.Media.Imaging.BitmapSource Render(System.Windows.Media.Visual Visual, int Resolution = 96)
{
System.Windows.Media.Imaging.RenderTargetBitmap render;
double width, height;
int pixelWidth, pixelHeight;
width = (double)Visual.GetValue(System.Windows.FrameworkElement.ActualWidthProperty);
height = (double)Visual.GetValue(System.Windows.FrameworkElement.ActualHeightProperty);
pixelWidth = Convert.ToInt32((width / 96) * Resolution);
pixelHeight = Convert.ToInt32((height / 96) * Resolution);
render = new System.Windows.Media.Imaging.RenderTargetBitmap(
pixelWidth, pixelHeight, Resolution, Resolution,
System.Windows.Media.PixelFormats.Pbgra32);
render.Render(Visual);
return render;
}
}
有人可以帮我解决这个问题吗?多谢你们!