Alexis
Alexis

Reputation: 13

Saving Image In UWP Then Loading In WinForms

Good morning StackOverflow,

I come to you today with a scenario that is driving me slowly insane. Im hopeful you can aid me with this, as I’m certain this must be possible but I can’t solve it myself.

My issue is that I’m working on two different applications at present. The first is a UWP application to deliver packages for an internal mail system. The idea here is that upon receipt of the package the person will sign the application using a InkCanvas signature. This should then be saved to the database as a byte array, and then reloaded in either a WinForm or WebForm app (I’m currently doing the WinForm one first) as a regular old image file. However, I’m absolutely stuck on converting between the WriteableBitmap that I get from UWP and the regular Bitmap I need to load in WinForms. Any ideas?

Here’s what I’m doing presently:

Saving the UWP image:

private byte[] SaveImage()
{
    var canvasStrokes = SignatureCanvas.InkPresenter.StrokeContainer.GetStrokes();

    if (canvasStrokes.Count > 0)
    {
        var width = (int) SignatureCanvas.ActualWidth;
        var height = (int) SignatureCanvas.ActualHeight;
        var device = CanvasDevice.GetSharedDevice();
        var renderTarget = new CanvasRenderTarget(device, width, height, 96);

        using (var drawingSession = renderTarget.CreateDrawingSession())
        {
            drawingSession.Clear(Colors.White);
            drawingSession.DrawInk(SignatureCanvas.InkPresenter.StrokeContainer.GetStrokes());
        }
        return renderTarget.GetPixelBytes();
    }
    return null;
}

Then I save the bytes to the database, and pull them from the database in the WinForms app... so am I making some boneheaded mistake here? Am I reading the signature in the wrong format? Or do I need to do something more to convert the formats from one to the other?

I’m stumped, after trying many different results from StackOverflow pages I don’t know what I’m doing wrong.

Any help would be amazing! And sorry if I’ve done something dumb.

Upvotes: 0

Views: 127

Answers (1)

Vincent
Vincent

Reputation: 3746

You're actually saving raw bitmap data to your database. I don't remember well how the Winform importer was working but I doubt it can import raw bitmap data.

You should encode your raw data to a PNG or JPEG image first and save the result. You will end with a regular old image file that should be readable from Winform.

using (IRandomAccessStream stream = /* the stream where you want to save the data */)
{
    byte[] bytes = renderTarget.GetPixelBytes();                

    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                 BitmapAlphaMode.Ignore,
                                 (uint)canvas.Width, (uint)canvas.Height,
                                 96, 96, bytes);

    await encoder.FlushAsync();
}

Upvotes: 1

Related Questions