C# 转换为jpeg图像的画布颜色错误

C# 转换为jpeg图像的画布颜色错误,c#,wpf-controls,jpeg,C#,Wpf Controls,Jpeg,我在应用程序中使用Windows.Control.Canvas为用户获取签名。然后我只想把这个签名保存为jpeg图片 然而,当我保存图片时,颜色似乎颠倒了 只是为了测试,我还尝试将图片保存为png-现在图片是它应该是 我正在使用此代码保存这些图片: public static void SaveCanvasToJPEG(Canvas canvas) { Rect bounds = VisualTreeHelper.GetDescendantBounds(canvas); Rend

我在应用程序中使用Windows.Control.Canvas为用户获取签名。然后我只想把这个签名保存为jpeg图片

然而,当我保存图片时,颜色似乎颠倒了 只是为了测试,我还尝试将图片保存为png-现在图片是它应该是

我正在使用此代码保存这些图片:

public static void SaveCanvasToJPEG(Canvas canvas)
{
    Rect bounds = VisualTreeHelper.GetDescendantBounds(canvas);
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width),
                                                    (int)(bounds.Height),
                                                    96d,
                                                    96d,
                                                    PixelFormats.Default);

    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(canvas);
        ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
    }
    rtb.Render(dv);

    //endcode as PNG
    JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder();
    FormatConvertedBitmap convertImg2GrayScale = new FormatConvertedBitmap(rtb, PixelFormats.Gray16, null, 0);
    jpegEncoder.Frames.Add(BitmapFrame.Create(convertImg2GrayScale));

    // *********** TESTING (begin) ************
    System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
    PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
    pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
    pngEncoder.Save(ms2);
    ms2.Close();
    System.IO.File.WriteAllBytes("logo.png", ms2.ToArray());
    // *********** TESTING (end) ************


    //save to memory stream
    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    jpegEncoder.Save(ms);
    ms.Close();

    System.IO.File.WriteAllBytes("logo.jpg", ms.ToArray());
}
以下是XML:

<UserControl x:Class="WPF_App.SignControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="600" d:DesignWidth="1252" Loaded="_eventUserControlLoaded">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="0*"/>
            </Grid.RowDefinitions>
            <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="330" Margin="159,149,0,0" VerticalAlignment="Top" Width="935"/>
            <Canvas x:Name="canSignPad" Focusable="True" HorizontalAlignment="Left" Height="328" Margin="160,150,0,0" 
                    ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Top" Width="933"
                    MouseDown="_canvasMouseDown" MouseMove="_canvasMouseMove">
                <Canvas.Background>
                    <SolidColorBrush Color="White" Opacity="0"/>
                </Canvas.Background>
            </Canvas>
     </Grid>
    </UserControl>


我发现很多关于jpeg图片的帖子都是蓝色的,但我没有找到解决问题的方法。

我只需要更改我的XAML定义:

    <Canvas.Background>
        <SolidColorBrush Color="White"/>
    </Canvas.Background>

如果要将画布保存为jpeg格式,请不要使用Opacity=“0”-因为jpeg不支持不透明度