我需要保存UniformGrid
中的图像。有可能吗?
我正在使用UniformGrid,因为如果我正在扩展Window,Image会适应UniformGrid的大小(负责)。你有什么想法吗?
答案 0 :(得分:1)
在WinForms
中可以选择将System.Drawing.Image
直接保存到文件中,如下所示:
MyImage.Save("C:\\myBitmap.bmp");
据我所知,这在WPF中是不可能的。但是有一种更动态的方式,使用RenderTargetBitmap
,您可以以图像的形式保存任何视觉元素。
在这个问题中:
Save content of a visual Object as a image file in WPF?
我找到了允许在JPG中保存视觉元素的方法。我将它们组合成一个静态类 WorkWithImages
:
public static class WorkWithImages
{
public static RenderTargetBitmap ConvertToBitmap(UIElement uiElement, double resolution)
{
var scale = resolution / 96d;
uiElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
var sz = uiElement.DesiredSize;
var rect = new Rect(sz);
uiElement.Arrange(rect);
var bmp = new RenderTargetBitmap((int)(scale * (rect.Width)), (int)(scale * (rect.Height)), scale * 96, scale * 96, PixelFormats.Default);
bmp.Render(uiElement);
return bmp;
}
public static void ConvertToJpeg(UIElement uiElement, string path, double resolution)
{
var jpegString = CreateJpeg(ConvertToBitmap(uiElement, resolution));
if (path != null)
{
try
{
using (var fileStream = File.Create(path))
{
using (var streamWriter = new StreamWriter(fileStream, Encoding.Default))
{
streamWriter.Write(jpegString);
streamWriter.Close();
}
fileStream.Close();
}
}
catch (Exception ex)
{
//TODO: handle exception here
}
}
}
private static string CreateJpeg(RenderTargetBitmap bitmap)
{
var jpeg = new JpegBitmapEncoder();
jpeg.Frames.Add(BitmapFrame.Create(bitmap));
string result;
using (var memoryStream = new MemoryStream())
{
jpeg.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
using (var streamReader = new StreamReader(memoryStream, Encoding.Default))
{
result = streamReader.ReadToEnd();
streamReader.Close();
}
memoryStream.Close();
}
return result;
}
}
这是一个完整的例子:
XAML
<Grid>
<Label Name="TestLabel"
Content="Test"
Width="100"
Height="100"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Beige" />
<StackPanel>
<Button Name="SaveToJpgButton"
Width="100"
Height="30"
Content="SaveToJpg"
Click="SaveToJpg_Click" />
</StackPanel>
</Grid>
<强> Code-behind
强>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SaveToJpg_Click(object sender, RoutedEventArgs e)
{
WorkWithImages.ConvertToJpeg(TestLabel, "TestLabel.jpg", 132);
}
}
public static class WorkWithImages
{
public static RenderTargetBitmap ConvertToBitmap(UIElement uiElement, double resolution)
{
var scale = resolution / 96d;
uiElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
var sz = uiElement.DesiredSize;
var rect = new Rect(sz);
uiElement.Arrange(rect);
var bmp = new RenderTargetBitmap((int)(scale * (rect.Width)), (int)(scale * (rect.Height)), scale * 96, scale * 96, PixelFormats.Default);
bmp.Render(uiElement);
return bmp;
}
public static void ConvertToJpeg(UIElement uiElement, string path, double resolution)
{
var jpegString = CreateJpeg(ConvertToBitmap(uiElement, resolution));
if (path != null)
{
try
{
using (var fileStream = File.Create(path))
{
using (var streamWriter = new StreamWriter(fileStream, Encoding.Default))
{
streamWriter.Write(jpegString);
streamWriter.Close();
}
fileStream.Close();
}
}
catch (Exception ex)
{
//TODO: handle exception here
}
}
}
private static string CreateJpeg(RenderTargetBitmap bitmap)
{
var jpeg = new JpegBitmapEncoder();
jpeg.Frames.Add(BitmapFrame.Create(bitmap));
string result;
using (var memoryStream = new MemoryStream())
{
jpeg.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
using (var streamReader = new StreamReader(memoryStream, Encoding.Default))
{
result = streamReader.ReadToEnd();
streamReader.Close();
}
memoryStream.Close();
}
return result;
}
}