Question

I use a Windows.Control.Canvas in my application to get the signiture for the user. then i just want to save this signiture as jpeg picture.

however, when i save the picture the color seems to be inverted just for testing i also tried to save the picture as png - now the picture is as it should be!

I am using this code to save these pictures:

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

an here is the 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>

i found a lot of posts talking about jpeg pictures come out blue, but i did not find a solution for my problem.

Was it helpful?

Solution

i just have to change my XAML definition:

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

do not use Opacity="0" when you want to save your canvas into a jpeg - cause jpeg did not support opacity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top