SkiaSharp Modify and save existing bitmap

I am trying to take an existing fingerpaint bitmap which is loaded onto the canvas, modify it by adding fingerpainting to it, and then save the modified bitmap. When I reload the newly saved bitmap it comes up as all black. Can anyone help?

    void SaveSketchToFile_EventHandler(object sender, EventArgs e)
    {

        // find and delete any existing file from local database
        IEnumerable<DocumentFile> binarySketches = App.database.GetSketches(sampleStationPassed.SampleStationLocalGuid);

        foreach(DocumentFile sketch in binarySketches)
        {
            App.Database.DeleteImageFromDatabase(sketch, sampleStationPassed.SampleStationLocalGuid);
        }


        // and now save what is on the canvas....


        // create non-visible surface in memory
        var info = new SKImageInfo((int)canvasView.CanvasSize.Width, (int)canvasView.CanvasSize.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
        var surface = SKSurface.Create(info);


        if(bitmapSketch != null)
        {
            using (var existingCanvas = new SKCanvas(bitmapSketch))
            {

                existingCanvas.Clear(SKColors.White);

                // draw original bitmap
                // Draw original in full size
                float x = (info.Width - bitmapSketch.Width) / 2;
                float y = (info.Height - bitmapSketch.Height) / 2;
                existingCanvas.DrawBitmap(bitmapSketch, x,y);


                // redraw objects on non-visible surface 
                foreach (PathAndSpecs pathSpecs in completedPathsWithSpecs)
                {
                    existingCanvas.DrawPath(pathSpecs.path, pathSpecs.paint);
                }

                foreach (PathAndSpecs pathSpecs in inProgressPathsWithSpecs.Values)
                {
                    existingCanvas.DrawPath(pathSpecs.path, pathSpecs.paint);
                }


                existingCanvas.Flush();

            }

        }
        else
        {
            using (var canvas = surface.Canvas)
            {

                canvas.Clear(SKColors.White);

                // redraw objects on noni ble surface 
                foreach (PathAndSpecs pathSpecs in completedPathsWithSpecs)
                {
                    canvas.DrawPath(pathSpecs.path, pathSpecs.paint);
                }

                foreach (PathAndSpecs pathSpecs in inProgressPathsWithSpecs.Values)
                {
                    canvas.DrawPath(pathSpecs.path, pathSpecs.paint);
                }

                canvas.Flush();
            }


        }

        canvasView.InvalidateSurface();

        // take a snapshot of the surface and return as an image
        var snap = surface.Snapshot();

        // encode the image as a certain file type or convert to byte array for storage
        SKData jpgImage = snap.Encode(SKEncodedImageFormat.Jpeg, 100);

        // convert image to byte array
        byte[] jpgByteArray = jpgImage.ToArray();


        // save image as jpg to local database where it will be stored for upload.
        CreateAndSavePic(jpgByteArray, "Site Sketch");



    }




    private void CreateAndSavePic(byte[] dbPhoto, string documentType)
    {

        DocumentSet docset = App.Database.PhotoDocumentSetExists(sampleStationPassed.SampleStationLocalGuid, documentType);

        if (docset is null) // create new documentSet for pics
        {
            // create new document set locally and update properties
            docset = new DocumentSet()
            {
                DocumentSetLocalGUID = DependencyService.Get<IDeviceMethods>().GUIDGenerator(),
                DeviceInfoID = App.deviceInfoID,
                DocumentSetObjectLocalGUID = sampleStationPassed.SampleStationLocalGuid,
                DocumentType = documentType,
                DocumentSetObject = "SampleStation",
                DocumentSetObjectID = sampleStationPassed.SampleStationID
                //Description = "Site images for this station visit_" + System.DateTime.Now.ToString() + "_" + sampleStationView.StationName

            };

            // save new photo to local database
            App.Database.SaveNewDocumentSet(docset);

        }


        DocumentFile newPhoto = new DocumentFile
        {
            BinaryObject = dbPhoto,

            // should be new one every time
            DocumentFileLocalGUID = DependencyService.Get<IDeviceMethods>().GUIDGenerator(),
            FileName = System.DateTime.Now.ToString() + "_Image",
            FileStorageType = "SQL Binary",
            FileType = "Portable Network Graphic",
            // DeviceInfoID = Convert.ToInt32(App.deviceInfoID),
            DocumentSetID = docset.DocumentSetID,
            DocumentSetLocalGUID = docset.DocumentSetLocalGUID

        };

        // save new photo to local database
        App.Database.SaveNewPhoto(newPhoto);

    }

Answers

  • PaulNTUPaulNTU USMember ✭✭✭

    hmm I think your tying yourself up in knots here. What is bitmapSketch? I presume this is from the database (Loaded image)

    Problem one
    In the if statement your checking if its null, if its not null you are creating a canvas with it.
    if(bitmapSketch != null)

    This is fine.
    Then you clear the canvas (Bitmap) and try to draw it back to itself. Blank on blank.
    Now you draw your lines which should work.

    **Problem number two
    You initialise the surface

    var surface = SKSurface.Create(info);

    Then after going into the If statement and modifying the existing bitmap (Not the surface). You call

    canvasView.InvalidateSurface();
    
    // take a snapshot of the surface and return as an image
    var snap = surface.Snapshot();
    

    The surface is blank because you didn't do anything to it. The content is on the bitmap not the surface. You need to save the bitmap. See problem one though as it will probably only show your new lines. Dont clear the existing bitmap, draw over it

    I hope that helps. I am having to work it out from the code so I hope I havent misunderstood some of the content :)

    Paul

Sign In or Register to comment.