uwp&wp8.1 Redraw picture WriteableBitmap usage picture to byte[] array, byte[] array to picture

Source: Internet
Author: User

---restore content starts---

WriteableBitmap is the most important way to reorganize pictures for UWP and WP8.1 to draw pictures. The method is simple and the method is diverse.

By viewing the document, WriteableBitmap's inheritance is writeablebitmap:bitmapsource "Bitmapsource:imagesource", which means WB " WriteableBitmap hereinafter referred to as "the source that can be directly assigned to the image control.

Usage Description:

WriteableBitmap wb=new WriteableBitmap (300,300) //This place is, width, length.  the int type.

Method 1SetSourceAsync ()

The way to do this is to use the Openasync stream of the file.

await Storagefile.getfilefrompathasync (FilePath);             await  Wb. Setsourceasync (await  file. OpenAsync (Fileaccessmode.readwrite));

Method 2 Pixelbuffer.asstream ().

This method is most commonly used for writing byte arrays.

The way to do this is to write the array through the stream stream.

            using (Stream _stream = wb.) Pixelbuffer.asstream ())            {                await0, buffer. Length);            }

The final step after you complete these steps is to redraw the entire picture. That's WB. Invalidate (); after which you assign a direct value to the image control's source.

Say one point:

If you have an array and want to redraw the image, when you create a new WB, the length and width must be the same as the width of the image, or as long as the control that displays the picture. Otherwise, there will be a flower screen, byte-length range and other errors.

If it is a binding picture, you do not directly use WB, you first create a new ImageSource, then assigned to the WB, in this imagesource to bind it can be

Ways to get an array of pictures:

The first is to open the file, in-app known files.

StorageFile ImageFile =awaitStoragefile.getfilefromapplicationuriasync (NewUri ("ms-appx:///ppap.jpg", Urikind.absolute)); Irandomaccessstream Accstream=awaitImagefile.openasync (Fileaccessmode.read); Bitmapdecoder BD=awaitBitmapdecoder.createasync (accstream);///Get Data            varImageData =awaitBd. Getpixeldataasync ();//            //get a two-dimensional array       byte[] buffer = Imagedata.detachpixeldata ();

There is another way, and the same

Randomaccessstreamreference ras = Randomaccessstreamreference.createfromuri (NewUri ("ms-appx:///ppap.jpg", Urikind.absolute)); varFiles =awaitRAS. OpenReadAsync ();                                                              //Decoding DeviceBitmapdecoder BD =awaitBitmapdecoder.createasync (Files. Clonestream ()); 
//Two ways to get data//buffer = new byte[(int) streamtoread.length]; //streamtoread.read (buffer, 0, (int) streamtoread.length); //await Streamtoread.readasync (buffer, 0, (int) streamtoread.length); //Bitmaptransform transform = new Bitmaptransform ()//compress or enlarge to be consistent with writeablebitmap length and width// //or equal to the size of the container showing the picture//{ //scaledheight =,//WB. Pixelheight; //scaledwidth = +,//WB. Pixelwidth; //}; //var imageData = await Bd. Getpixeldataasync (Bitmappixelformat.bgra8, bitmapalphamode.straight, transform, Exiforientationmode.ignoreexiforientation, colormanagementmode.donotcolormanage); ///Get Data varImageData =awaitBd. Getpixeldataasync ();// //get a two-dimensional array byte[] buffer = Imagedata.detachpixeldata ();

uwp&wp8.1 Redraw picture WriteableBitmap usage picture to byte[] array, byte[] array to picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.