The previous article described the use of C # to generate a QR code, followed by a further addition of functionality.
1. Save the generated QR code to the cost of the picture
the key to this operation is how to read the resource in the image control and save it locally.
1.1 Creates an BitmapSource object that represents a single invariant set of pixels with a specific size and resolution.
1.2 Creates an Bitmapencoder object that encodes a collection of Bitmapfram objects as an image stream.
1.3 Use the Bitmapframe.create method to create a new bitmapsource from a given BitmapSource
The 1.4 FileStream class, which exposes a file-based stream, supports both synchronous read and write operations as well as asynchronous read and write operations. Public FileStream (string path, FileMode mode).
1.5 Sets the frames within the image by BitmapEncoder.Frames.Add ().
1.6 encode a bitmap image as a specified stream by using the Bitmapencoder.save method
BitmapSource m = (bitmapsource) image1. Source;
Bitmapencoder encoder = null;
encoder = new Pngbitmapencoder ();
FileStream stream = new FileStream ("5555.png", FileMode.Create);
Encoder. Frames.add (Bitmapframe.create (m));
Encoder. Save (stream);
2. Display the selected local QR code picture in the image control
2.1 Defines a OpenFileDialog object that represents a common dialog box that users can use to specify the file name of one or more files to open.
2.2 Set the text displayed in the title bar of the file dialog box via the Openfiledialog.title property;
set whether the user is allowed to select multiple files via the Openfiledialog.multiselect property;
set the filter string through the Openfiledialog.filter property;
sets a string with the Openfiledialog.filename property that contains the full path of the file selected in the file dialog box.
2.3 Declares an object of the BitmapImage class that provides a dedicated bitmapsource that is optimized to upload images using XAML.
2.4 Finally, copy the source of the image control on WPF.
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog ();
dlg. Title = "Please select image!"; /Set Title
dlg. MultiSelect = false;//No multiple selection
dlg. InitialDirectory = initialpath;//Set Default path
dlg. Filter = "Picture |*.jpg;*.png;*.gif;*.bmp;*.jpeg";
dlg. ShowDialog ();
string Szpath =dlg. FileName;
BitmapImage BitmapImage = new BitmapImage ();
bitmapimage.begininit ();
bitmapimage.cacheoption = bitmapcacheoption.onload;
Bitmapimage.urisource = new Uri (szpath);
bitmapimage.endinit ();
Bitmapimage.freeze ();
Image1. Source = BitmapImage;
3. Identify the image of the QR code displayed in the image control
The key point is to convert the image in the WPF image control to bitmap
3.1 Declare a Barcodereader object, a smart class to decode a bitmap object inside the barcode or the QR code;
Use the Barcodereader.decode (Bitmap) method to decrypt the barcode or QR code of a bitmap object;
The return value of Barcodereader.decode (BITMAP) is the result type and the parameter is the Bitmap type.
3.2 Creates an BitmapSource object m, obtains the source in the image control, assigns the value m;
Create a Bitmap object BMP, call the public Bitmap (int width, int height, pixelformat format) constructor to create a new instance of the specified size and format Bitmap class.
call the Bitmap.lockbits method to lock the bitmap into system memory.
call the Bitmapsource.copypixels method to copy bit-image-voxel data.
call the Bitmap.unlockbits method to unlock this biamap from system memory.
3.3 Call the Result.tostring () method to display the string that reads the QR code.
Barcodereader Reader =new Barcodereader ();
BitmapSource m = (bitmapsource) image1. Source;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap (M.pixelwidth, M.pixelheight, SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT32BPPPARGB);
System.Drawing.Imaging.BitmapData data = bmp. LockBits (New System.Drawing.Rectangle (System.Drawing.Point.Empty, BMP. Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
m.copypixels (int32rect.empty, data. Scan0, data. Height * data. Stride, data. Stride);
bmp. Unlockbits (data);
result result = reader. Decode (BMP);
MessageBox.Show (result. ToString ());
***************************************************************************************************************
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
WPF operation QR Code (II)