For this, I am learning the problem of inserting pictures, in the Windows application directly with the image class and bitmap directly implement the cropping function,
Not on WP, and later on the MSDN Forum found the relevant methods and examples, and the example is too complex,
I'll refer to a simple example on https://social.msdn.microsoft.com/Forums/zh-CN/07b06a88-607b-44d8-ae5c-801d5c8b3a27,
Because the sample does not have too many annotations, I have to cooperate with the development of the program on the annotation and their own understanding, convenient for everyone;
Using the WriteableBitmap class, Bitmapdecoder class, good directly on the example:
1. The size of the source image: 140 pixels wide, 37 pixels high;
2. The size of the image we want to crop is: 28 pixels wide, 37 pixels high (equivalent to dividing the source image along a straight line 5 copies);
3. Knowing the requirements of the last two, we will first create a method for obtaining pixel data after cropping the picture (28 pixels wide and 37 pixels high), the code is as follows:
1 #region2 /// <summary>3 ///Create a Bitmapdecoder pixel converter and set properties for the transform (crop border, scale size)4 /// </summary>5 /// <param name= "Decoder" >the decoded data to be modified</param>6 /// <param name= "StartX" >crop Border top left corner x position</param>7 /// <param name= "Starty" >crop border upper left corner y position</param>8 /// <param name= "width" >Crop Border Width</param>9 /// <param name= "height" >crop Border Height</param>Ten /// <param name= "Scaledwidth" >pixel width after zooming</param> One /// <param name= "Scaledheight" >the pixel height after zooming</param> A /// <returns></returns> - Public Static Asynctask<byte[]> Getpixeldata (Bitmapdecoder decoder,UINTStartX,UINTStarty,UINTWidthUINTHeightUINTScaledwidth,UINTscaledheight) - { the - - //gets the trimmed border size, specifying the upper-left corner position (Startx,starty) and the clipping height height and width widths; - #regionDefine clipping border size; +Bitmapbounds bound =Newbitmapbounds (); -Bound. X =StartX; +Bound. Y =Starty; ABound. Width =width; atBound. Height =height; - #endregion - - //converters that create pixel data for bitmaps - #regionDefine Converter Properties -Bitmaptransform Tran =Newbitmaptransform (); in - //bound the attributes of the cropped dimension to Tran. In Bounds toTran. Bounds =bound; + - //define the converter to scale the size, theTran. Scaledwidth =Scaledwidth; *Tran. Scaledheight =Scaledheight; $ #endregionPanax Notoginseng - //Get pixel data the varv =awaitdecoder. Getpixeldataasync (Bitmappixelformat.bgra8, Bitmapalphamode.straight, Tran, Exiforientationmode.ignoreexiforientation, Colormanagementmode.colormanagetosrgb); + //get binary pixel data from pixel data A byte[] pixels =V.detachpixeldata (); the returnPixels//return + } - #endregion
View Code
4. Next, the pixel data obtained from the previous code is clipped as required and returns a WriteableBitmap type of BitmapSource:
1 Public Async StaticTask<writeablebitmap>get_image ()2 {3 4 5 //get "picture. bmp" Picture file6StorageFile ImageFile =awaitStoragefile.getfilefromapplicationuriasync (NewUri ("ms-appx:///petsdata/pictures. bmp"));7 //Create a random access stream, read the picture data, and put it into the stream8 using(Irandomaccessstream stream =awaitImagefile.openasync (fileaccessmode.read))9 {Ten //x, y represents the upper-left corner of the picture, width and height represent the size of the cropped border One UINTx =0; A UINTy =0; - UINTwidth = -; - UINTHeight =Panax Notoginseng; the - //Decoding the data stream with a decoding class -Bitmapdecoder decoder =awaitBitmapdecoder.createasync (stream); - //Modified the obtained decoded pixel data stream by border x, y, width, height size; + //since the source picture is not scaled, the Getpixeldata () method immediately assigns the size pixel decoder of the source picture directly after the two-bit parameter. Pixelwidth, decoder. Pixelheight; - byte[] pixels =awaitGetpixeldata (decoder, x, y, width, height, decoder. Pixelwidth, decoder. Pixelheight); + //creation of writable bitmapsource WP; AWriteableBitmap WB =NewWriteableBitmap ( -,Panax Notoginseng); atStream Pixstream =WB. Pixelbuffer.asstream (); - //the size of the pixel data written to the clip; -Pixstream.write (Pixels,0, pixels. Length); - //returns the BitmapSource of the WriteableBitmap type - returnWB; - } in -}
View Code
5. Then assign a value to the source of the image control named "IMG1"
This await Get_image ();
6. This is done;
Finally, this article does not use the coding class Bitmapencoder, when use I do not know now, hope the great God Guide;
WP8.1 Development to crop (intercept) part of a picture