【问题标题】:Saving Images from Image Control isn't working从图像控制保存图像不起作用
【发布时间】:2012-05-22 10:57:13
【问题描述】:

我的 GUI 上有一个图像(框架元素)。 里面有一张图。现在我在这张图片上双击,我想要, 图像会自行保存,并将使用默认图像查看器打开。

我的代码:

void image_MouseDown(object sender, MouseButtonEventArgs e)
{
    //Wayaround, cause there is no DoubleClick Event on Image 
    if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
    {
        SaveToPng(((Image)sender), "SavedPicture.png");
        Process.Start("SavedPicture.png");
    }
}

void SaveToPng(FrameworkElement visual, string fileName)
{
    var encoder = new PngBitmapEncoder();
    SaveUsingEncoder(visual, fileName, encoder);
}

void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
    RenderTargetBitmap bitmap = new RenderTargetBitmap(
        (int)visual.ActualWidth,
        (int)visual.ActualHeight,
        96,
        96,
        PixelFormats.Pbgra32);
    bitmap.Render(visual);
    BitmapFrame frame = BitmapFrame.Create(bitmap);
    encoder.Frames.Add(frame);

    using (var stream = File.Create(fileName))
    {
        encoder.Save(stream);
    }
}

使用Process.Start 打开图片效果很好。问题是保存,它保存了图片:SavedPicture.png 但是,它只是黑色,所以没有图形.. 也许有人可以告诉我,我的代码有什么问题,或者知道在 WPF 中保存图像的更好方法。

【问题讨论】:

    标签: c# .net wpf image save


    【解决方案1】:

    在保存之前必须显示图像。因此,如果您想使用RenderTargetBitmap,只需设置Image.Source 并在使用SaveToPng 保存之前加载ImageActualWidthActualHeight 不能为空)。

    示例

    如果您在Panel 中有Image

    <Grid x:Name="MyGrid">
        <Image x:Name="MyImage"/>
    </Grid>
    

    我在我的测试类构造函数中设置了Image.Source,只有在图像加载后我才保存它:

    public MainWindow()
    {
        InitializeComponent();
    
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.UriSource = new Uri("image.png", UriKind.RelativeOrAbsolute);
        bmp.EndInit();
        MyImage.Source = bmp;
    
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        RenderTargetBitmap bmp = new RenderTargetBitmap((int)MyGrid.ActualWidth,
                (int)MyGrid.ActualHeight, 96, 96, PixelFormats.Default);
    
        bmp.Render(MyImage);
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bmp));
    
        using (var stream = System.IO.File.Create("newimage.png"))
        { encoder.Save(stream); }
    }
    

    如果您不想使用 Grid ActualWidthActualHeight,只需将您的 with 和 height 作为参数传递。

    【讨论】:

    • 显示图像。我不需要使用RenderTargetBitmap,我问有没有更简单更好的方法。您能否提供一些示例代码,说明您将 Image.Source 设置为 RenderTargetBitmap 的实际含义?谢谢
    • 结果是一样的。它保存了一个“newimage.png”,但是当我用任何 Imageviewer 打开它时,它是纯色的或只是黑色的 - 没有内容。
    【解决方案2】:

    取决于 Image.Source 的类型,假设您有文章中的 BitmapSource,它应该是这样的:

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
    using (FileStream stream = new FileStream(filePath, FileMode.Create))
        encoder.Save(stream);
    

    顺便说一句,RenderTargetBitmap 类是将 Visual 对象转换为位图。这是团队推荐的

    样本 http://msdn.microsoft.com/en-us/library/aa969819.aspx

    【讨论】:

    • 只是出于好奇,什么是团队?
    【解决方案3】:

    问题解决了。

    我用过这个:File.WriteAllBytes() 从二进制格式保存图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-14
      • 2011-07-26
      • 2020-11-04
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多