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