This article tells you how to use win2d to add watermarks to images.
Installation
First you need to install win2d using Nuget, see Win10 UWP win2d
If you do not update dot net core then the following exception may occur in the runSystem.TypeLoadException: Requested Windows Runtime type ‘Microsoft.Graphics.Canvas.Text.CanvasTextLayout‘ is not registered
Then directly update dot NET core to the latest, and then clean up the project to
Get Pictures
To deal with the picture, the first need to get the picture, the way to get the picture can be obtained from the Clipboard or using file selection.
If you get the picture from the Clipboard, you need to save the picture to a local temp folder and then get the file.
If you use file selection to get the file, you can use this method
var pick = new FileOpenPicker(); pick.FileTypeFilter.Add(".jpg"); pick.FileTypeFilter.Add(".png"); var file = await pick.PickSingleFileAsync();
Note that the suffix name uses the .
+ suffix name, here I write is very few picture suffix name, actually can support the picture suffix is many.
Create a picture
If you need to process the picture, using Canvasrendertarget, you can see that this class needs to pass in two parameters, ICanvasResourceCreatorWithDpi
Size
I will use this function
In the win2d to use the picture needs Canvasbitmap, this class can not be directly created, need to pass,,, LoadAsync
CreateFromBytes
CreateFromColors
CreateFromSoftwareBitmap
These methods to create, the following is created using the first method.
The first method has many overloads, and it is important to note that if the file is not in the solution, do not use the filename or URI method because the file cannot be accessed frequently.
If the file is not in the solution, do not use the filename or URI method to read the picture, because the general file does not have permissions. Even if you use Filepick to get the file, the path to the file may not be available.
The recommended method is to use the overload of the flow, on the above, has already got the file, then the file read out, incoming can
var duvDbecdgiu = await CanvasBitmap.LoadAsync(new CanvasDevice(true), await _file.OpenAsync(FileAccessMode.Read));
Working with pictures
Now create canvasrendertarget processing picture, in use Canvasrendertarget remember release, so generally need to use the following code
using (var canvasRenderTarget = new CanvasRenderTarget(duvDbecdgiu, duvDbecdgiu.Size))
Create a picture processing that is the same size as the picture.
The way to add text in a picture is actually the same as the other processing in win2d, you can go to see my win2d blog.
using (var dc = canvasRenderTarget.CreateDrawingSession()) { dc.DrawImage(duvDbecdgiu); dc.DrawText("lindexi", new Vector2((float) (duvDbecdgiu.Size.Width / 2), (float) duvDbecdgiu.Size.Height/2), Colors.Black); }
Perhaps people will think that the above DrawImage
is what to do, just not created from the picture? Actually created from the picture, but no picture is drawn, that is, when you use it, you need to draw the picture first and then draw the text.
Save
Now try to save a picture, save need to let the user choose a file
var pick = new FileSavePicker(); pick.FileTypeChoices.Add("image", new List<string>() {".jpg"}); var file = await pick.PickSaveFileAsync();
Saving is simple
await canvasRenderTarget.SaveAsync(await file.OpenAsync(FileAccessMode.ReadWrite),CanvasBitmapFileFormat.Jpeg);
Note the saved format can be many, but the suffix name needs to be the same as the saved format.
Now this function is written on the graph bed
Welcome everyone to visit my blog win2d image watermark I build my own blog will be constantly updated
This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Welcome to reprint, use, republish, but be sure to keep the article Attribution Lindesi (including Link: http://blog.csdn.net/lindexi_gd), not for commercial purposes, based on the modified works of this article must be issued with the same license. If you have any questions, please contact me.
win2d Image Watermark