public static async Task <HLinkMediaModel> CreateClippedMediaModel(HLinkLoadImageModel argHLinkLoadImageModel) { if (argHLinkLoadImageModel is null) { throw new ArgumentNullException(nameof(argHLinkLoadImageModel)); } if (!argHLinkLoadImageModel.DeRef.Valid) { throw new ArgumentException("CreateClippedMediaModel argument is invalid", nameof(argHLinkLoadImageModel)); } // TODO cleanup code. Multiple copies of things in use MediaModel theMediaModel = argHLinkLoadImageModel.DeRef; SKBitmap resourceBitmap = new SKBitmap(); MediaModel newMediaModel = new MediaModel(); string newHLinkKey = argHLinkLoadImageModel.HLinkKey + "-" + argHLinkLoadImageModel.GCorner1X + argHLinkLoadImageModel.GCorner1Y + argHLinkLoadImageModel.GCorner2X + argHLinkLoadImageModel.GCorner2Y; string outFileName = Path.Combine("Cropped", newHLinkKey + ".png"); string outFilePath = Path.Combine(DataStore.AD.CurrentDataFolder.FullName, outFileName); Debug.WriteLine(argHLinkLoadImageModel.DeRef.MediaStorageFilePath); // Check if already exists MediaModel fileExists = DV.MediaDV.GetModelFromHLinkString(newHLinkKey); if (!fileExists.Valid) { // Needs clipping using (StreamReader stream = new StreamReader(theMediaModel.MediaStorageFilePath)) { resourceBitmap = SKBitmap.Decode(stream.BaseStream); } // Check for too large a bitmap Debug.WriteLine("Image ResourceBitmap size: " + resourceBitmap.ByteCount); if (resourceBitmap.ByteCount > int.MaxValue - 1000) { // TODO Handle this better. Perhaps resize? Delete for now resourceBitmap = new SKBitmap(); } float crleft = (float)(argHLinkLoadImageModel.GCorner1X / 100d * theMediaModel.MetaDataWidth); float crright = (float)(argHLinkLoadImageModel.GCorner2X / 100d * theMediaModel.MetaDataWidth); float crtop = (float)(argHLinkLoadImageModel.GCorner1Y / 100d * theMediaModel.MetaDataHeight); float crbottom = (float)(argHLinkLoadImageModel.GCorner2Y / 100d * theMediaModel.MetaDataHeight); SKRect cropRect = new SKRect(crleft, crtop, crright, crbottom); SKBitmap croppedBitmap = new SKBitmap( (int)cropRect.Width, (int)cropRect.Height ); SKRect dest = new SKRect( 0, 0, cropRect.Width, cropRect.Height ); SKRect source = new SKRect( cropRect.Left, cropRect.Top, cropRect.Right, cropRect.Bottom); using (SKCanvas canvas = new SKCanvas(croppedBitmap)) { canvas.DrawBitmap(resourceBitmap, source, dest); } // create an image COPY SKImage image = SKImage.FromBitmap(croppedBitmap); // encode the image (defaults to PNG) SKData encoded = image.Encode(); // get a stream over the encoded data using (Stream stream = File.Open(outFilePath, FileMode.OpenOrCreate, System.IO.FileAccess.Write, FileShare.ReadWrite)) { encoded.SaveTo(stream); } croppedBitmap.Dispose(); // ------------ Save new MediaObject newMediaModel = theMediaModel.Copy(); newMediaModel.HLinkKey = newHLinkKey; newMediaModel.HomeImageHLink.HLinkKey = newHLinkKey; newMediaModel.HomeImageHLink.HomeImageType = CommonConstants.HomeImageTypeThumbNail; newMediaModel.HomeImageHLink.HomeSymbol = CommonConstants.IconMedia; newMediaModel.OriginalFilePath = outFileName; newMediaModel.MediaStorageFile = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, outFileName).ConfigureAwait(false); newMediaModel.HomeImageHLink.HomeImageType = CommonConstants.HomeImageTypeThumbNail; newMediaModel.IsClippedFile = true; newMediaModel.MetaDataHeight = cropRect.Height; newMediaModel.MetaDataWidth = cropRect.Width; DataStore.DS.MediaData.Add(newMediaModel); //await StorePostLoad.fixMediaFile(newMediaModel).ConfigureAwait(false); } resourceBitmap.Dispose(); return(newMediaModel.HLink); }