Rendertargetbitmap inherits from the bitmapsource type and defines two important methods: clear and render. One is to set all the images to black and transparent, and the other is to output the visual objects in WPF to the image.
The parameters of the rendertargetbitmap constructor are common properties for building bitmap images: pixel size, dpix, dpiy, and pixel storage format. For normal image storage, use the default value. DPI-related parameters are 0 (0 indicates that the default value is used, and the DPI of the final image cannot be 0, usually 96 by default). The pixel storage format is pixelformats. default (corresponding to pbgra32 ).
Drawingvisual inherits from the containervisual type and defines a very powerful method: rrenderopen, which returns a drawingcontext. The latter is similar to the device context in GDI and can be used to output graphic data to the graphic terminal.
You can use rendertargetbitmap and drawingvisual to add watermarks to images.Program.
The effect is as follows:
Original Image:
After running the program:
The following programCodeIn addition to common namespaces in WPF, you also need to add the following namespaces:
UsingSystem.Windows.Media.Imaging;
// WPF image type
UsingSystem.Io;
// File/path related operations
First, read the image file based on the image path, that is, create the basic bitmapsource object of the image in WPF, then create the rendertargetbitmap object, and use the attributes of the image in bitmapsource. The last pixel type has a special case. pixelformats. Default must be used instead of the format attribute of bitmapsource. Currently, rendertargetbitmap only supports the pbgra32 type. Sometimes bitmapsource format is bgr32 or another pixel format.
Code:
// Image file path
VaRPath= @ "E: \ Users \ mgen \ pictures \ mgenx.jpg";
// Read the image
BitmapsourceBitmap= New Bitmapimage(New Uri(Path,Urikind.Absolute ));
// Create rendertargetbitmap
VaRRtbitmap= New Rendertargetbitmap(Bitmap.Pixelwidth,
Bitmap.Pixelheight,
Bitmap.Dpix,
Bitmap.Dpiy,
Pixelformats.Default );
Next we need to build the interface element, because the core of rendertargetbitmap is to output the visual to the image, then we start to build the visual. In this case, use drawingvisual and then use the renderopen method to obtain drawingcontext and output the image and text.
First, create a formattedtext object as the watermark:
// Generate formattedtext
VaRText= New Formattedtext("Mgen! ",
System.Globalization.Cultureinfo.Currentculture,
System.Windows.Flowdirection.Lefttoright,
New Typeface(Systemfonts.Messagefontfamily,Fontstyles.Normal,Fontweights.Bold,Fontstretches.Normal ),
24,
Brushes.White );
Next, use drawingvisual and drawingcontext to plot and output the image and formattedtext:
// Drawingvisual and drawingcontext
VaRDrawingvisual= New Drawingvisual();
Using(VaRDC=Drawingvisual.Renderopen ())
{
DC.Drawimage (bitmap,New Rect(0,0, Bitmap.Width, bitmap.Height ));
DC . Drawrectangle ( Brushes . Transparent, New Pen ( Brushes . White, 7 ), New Rect ( 10 , 10 , 20 + Text . Width, 20 + Text . Height ));
DC.Drawtext (text,New Point(20,20));
}
OK. After that, you can call the render method of rendertargetbitmap and pass in the drawingvisual object just now:
// Call the rendertargetbitmap render
Rtbitmap.Render (drawingvisual );
Finally, use bitmapdecoder to save rendertargetbitmap (which also belongs to bitmapsource) to the file:
// Create a decoder
VaRBitmapencoder= New Jpegbitmapencoder();
// Add the first frame
Bitmapencoder.Frames.Add (Bitmapframe.Create (rtbitmap ));
// Save to file (Save the modified image to the program directory without modifying the source file)
Bitmapencoder.Save (File.Openwrite (Path.Getfilename (PATH )));