获取所有图片并将其保存到文件中

时间:2012-10-22 17:03:17

标签: c# wpf image printscreen uniformgrid

我有一个包含所有视频缩略图的UniformGrid(它们都是System.Windows.Control.Image)。我的目标是在点击按钮后保存所有缩略图的jpg。有没有办法从UbiformGrid中获取位图图像? 我在WPF中使用C#。

编辑:就像截图一样。但我不想窗口边框,只有网格内容。

Edit2:我终于找到了解决方案。谢谢您的帮助。

RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)ThumbnailPanel.Width, 

(int)ThumbnailPanel.Height, 96, 96, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(ThumbnailPanel);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
    drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(ThumbnailPanel.Width, ThumbnailPanel.Height)));
}
renderTarget.Render(drawingVisual);
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = 80;
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] _imageArray;
using (MemoryStream outputStream = new MemoryStream())
{
    jpgEncoder.Save(outputStream);
    _imageArray = outputStream.ToArray();

}
FileStream fileStream = new FileStream(@"myThumbnails.jpg", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);
binaryWriter.Write(_imageArray);
binaryWriter.Close();

1 个答案:

答案 0 :(得分:0)

当然,只需使用forforeach遍历您的图片集合,然后使用System.Drawing命名空间中的Image.GetThumbnailImage方法创建缩略图......就这么简单

例如:

foreach (var img in myImages)
{
  var thumb = image.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
  //Do something with the thumbnail
  thumb.Save(output)
}