C# 保存照片效果

C# 保存照片效果,c#,wpf,bitmap,save,effect,C#,Wpf,Bitmap,Save,Effect,因此,我有代码来保存我用drop shadow编辑的图像,例如,保存后,我发现代码只保存图像大小的文件。我需要的是使用新的大小保存,对图像的影响应该会变大,例如,因为它下面的阴影。 我不知道如何编辑代码来保存更大的图像。如果图像静态地更大,它真的没有问题。例如,如果它保存图像的大小+每个大小20个像素,我不介意 try { Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog();

因此,我有代码来保存我用drop shadow编辑的图像,例如,保存后,我发现代码只保存图像大小的文件。我需要的是使用新的大小保存,对图像的影响应该会变大,例如,因为它下面的阴影。 我不知道如何编辑代码来保存更大的图像。如果图像静态地更大,它真的没有问题。例如,如果它保存图像的大小+每个大小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;
}

请尝试在调用System.IO.File.Open时使用FileMode.Create而不是FileMode.Open或Create

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

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;
        }

而且,你使用剪贴板加载图像有什么原因吗?

我更改了它,但仍然没有解决我的问题……我不知道为什么我会尝试你的建议,并最终得到这张便笺,如果我尝试在两个方向上都使用20,右边的水线会变小,但它仍然只是那一行:黑色的线始终在保存的文件中除了PNG格式,如果不知道image1是如何构造的,以及它如何与应用程序的其余部分联系在一起,就很难预测结果。黑色是新图像的背景色-在PNG中是不可见的,因为它在那里是透明的。根据image1的上下文,在渲染图像之前,有多种方法可以用白色或任何其他颜色填充新图像。我所寻找的只是周围有外部辉光效果的图片。我得到了很多-但你必须了解位图PNG/JPG。。没有有效的像素。外部辉光效果将添加到图像的外部。因此,它只能由查看应用程序应用,或者它必须是图像本身的一部分。如果它是图像的一部分,它所操作的背景也必须是图像的一部分。即白色背景,图像+白色背景上的阴影。JPG输出看起来很奇怪,因为在执行保存操作之前,WPF将使用半透明像素,保存时这些像素会变得不透明。你能分享image1的代码吗?这是XAML代码,因为在c代码上什么都没有。