WPF保存照片效果

时间:2014-01-04 11:52:03

标签: c# wpf bitmap save effect

所以我有代码保存我用投影编辑的图像,例如我保存后我发现代码只保存了图像大小的文件。我需要的是保存新的效果的大小(图像应该变大,因为它下面的阴影)。 我不知道如何编辑代码来保存更大的图像。如果图像是静态更大它真的没问题我不介意它是否保存图像的大小+每个尺寸20像素。例如。

try
{
    Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
    saveDialog.Filter = "JPeg Image(*.JPG)|*.jpg|Bitmap Image(*.BMP)|*.bmp|Png Image(*.PNG)|*.png|Gif Image(*.GIF)|*.gif";
    if (saveDialog.ShowDialog().Value == true)
    {
        // Save current canvas transform
        Transform transform = image1.LayoutTransform;
        // reset current transform (in case it is scaled or rotated)
        image1.LayoutTransform = null;

        // Get the size of canvas
        Size size = new Size(image1.ActualWidth, image1.ActualHeight);
        // Measure and arrange the surface
        // VERY IMPORTANT
        image1.Measure(size);
        image1.Arrange(new Rect(size));

        // Create a render bitmap and push the surface to it
        RenderTargetBitmap renderBitmap =
          new RenderTargetBitmap(
            (int)size.Width,
            (int)size.Height,
            96d,
            96d,
            PixelFormats.Default);
        renderBitmap.Render(image1);
        BitmapEncoder encoder = new BmpBitmapEncoder();
        string extension = saveDialog.FileName.Substring(saveDialog.FileName.LastIndexOf('.'));
        switch (extension.ToLower())
        {
            case ".jpg":
                encoder = new JpegBitmapEncoder();
                break;
            case ".bmp":
                encoder = new BmpBitmapEncoder();
                break;
            case ".gif":
                encoder = new GifBitmapEncoder();
                break;
            case ".png":
                encoder = new PngBitmapEncoder();
                break;
        }
        // push the rendered bitmap to it
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        // Create a file stream for saving image
        using (System.IO.FileStream fs = System.IO.File.Open(saveDialog.FileName, System.IO.FileMode.OpenOrCreate))
        {
            encoder.Save(fs);
        }
        // Restore previously saved layout
        image1.LayoutTransform = transform;
    }

}
catch (Exception)
{

    throw;
}

2 个答案:

答案 0 :(得分:0)

请在FileMode.Create

的来电中尝试使用FileMode.OpenOrCreate代替System.IO.File.Open()

答案 1 :(得分:0)

如果您的意思是在WPF中为图像添加了“位图效果”或“着色器”,则可以预期结果,因为这些不是布局过程的一部分,并且没有考虑到“ActualWidth /”的ActualHeight”。您可以在创建新图像时添加几个单位:

Size size = new Size(image1.ActualWidth + 20, image1.ActualHeight + 20);

如果“image1”对象的其他布局属性是正确的,那么这应该可以解决问题。

编辑:我建议使用新控件渲染图像,而不是使用属于不同可视树的控件。布局/渲染可能有各种奇怪的副作用。这是一段适用于投影效果的代码:

编辑2:将“效果”属性设置为与UI中相同的对象...

        try
        {
            Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();
            saveDialog.Filter = "JPeg Image(*.JPG)|*.jpg|Bitmap Image(*.BMP)|*.bmp|Png Image(*.PNG)|*.png|Gif Image(*.GIF)|*.gif";
            if (saveDialog.ShowDialog().Value == true)
            {
                Image image2 = new Image();
                image2.Source = image1.Source;
                Grid container = new Grid();
                container.Children.Add(image2);
                container.Background = new SolidColorBrush(Colors.White);
                image2.Stretch = Stretch.None;
                image2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                image2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                // Get the size of canvas
                Size size = new Size(image2.Source.Width + 20, image2.Source.Height + 20);
                // Measure and arrange the surface
                // VERY IMPORTANT
                container.Measure(size);
                container.Arrange(new Rect(size));

                // BitmapEffect is deprecated and buggy, use Effect instead.

                // since the effect isn't part of the visual tree but independent,
                // we can just reuse the same object as on the image in the UI.
                image2.Effect = image1.Effect;

                //image2.Effect = new DropShadowEffect
                //{
                //    Color = Colors.Black,
                //    ShadowDepth = 5,
                //    BlurRadius = 3,
                //    Opacity = 1
                //};


                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Default);

                renderBitmap.Render(container);
                BitmapEncoder encoder = new BmpBitmapEncoder();
                string extension = saveDialog.FileName.Substring(saveDialog.FileName.LastIndexOf('.'));
                switch (extension.ToLower())
                {
                    case ".jpg":
                        encoder = new JpegBitmapEncoder();
                        break;
                    case ".bmp":
                        encoder = new BmpBitmapEncoder();
                        break;
                    case ".gif":
                        encoder = new GifBitmapEncoder();
                        break;
                    case ".png":
                        encoder = new PngBitmapEncoder();
                        break;
                }
                // push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                // Create a file stream for saving image
                using (System.IO.FileStream fs = System.IO.File.Open(saveDialog.FileName, System.IO.FileMode.OpenOrCreate))
                {
                    encoder.Save(fs);
                }
            }

        }
        catch (Exception)
        {

            throw;
        }

另外,您是否有理由使用剪贴板加载图片?