Original: Cut/transduction/cropped picture of "C#/WPF" picture
The front desk prepares two image controls. Above is the display of the original, the following shows the effect after cutting.
<StackPanel Orientation="Vertical"> <Image Width=" 383 " Height= "=" Source="c \ Users\administrator\documents\visual Studio 2015\projects\splitpic\splitpic\images\1.jpg "/> <Image x:name="img" Stretch="None" Width="450" Height="383" /></StackPanel>
The corresponding background code:
Public Partial classmainwindow:window{ Public MainWindow() {InitializeComponent ();//Set the originalImg. Source =NewBitmapImage (NewUri (@ "Images/1.jpg", urikind.relative));//Cut picturesImageSource ImageSource = img. Source; Bitmap Bitmap = Systemutils.imagesourcetobitmap (ImageSource); BitmapSource BitmapSource = systemutils.bitmaptobitmapimage (bitmap); BitmapSource Newbitmapsource = Systemutils.cutimage (BitmapSource,NewInt32Rect ( the, -,235,285));//Using a cut-out diagram sourceImg. Source = Newbitmapsource; }}//Image tool class Public Static classsystemutils{// <summary> /// cut Chart // </summary> /// <param name= "BitmapSource" > map source </param> /// <param name= "cut" > cutting area </param> /// <returns></returns> Public StaticBitmapSourceCutimage(BitmapSource BitmapSource, Int32Rect cut) {//Calculation Stride varStride = BitmapSource.Format.BitsPerPixel * cut. Width/8;//Declare byte array byte[] data =New byte[Cut. Height * Stride];//Call CopyPixelsBitmapsource.copypixels (cut, data, stride,0);returnBitmapsource.create (cut. Width, cut. Height,0,0, Pixelformats.bgr32,NULL, data, stride); }//ImageSource-Bitmap Public StaticSystem.Drawing.BitmapImagesourcetobitmap(ImageSource ImageSource) {BitmapSource m = (bitmapsource) ImageSource; System.Drawing.Bitmap BMP =NewSystem.Drawing.Bitmap (M.pixelwidth, M.pixelheight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); System.Drawing.Imaging.BitmapData data = bmp. LockBits (NewSystem.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);returnBmp }//Bitmap-BitmapImage Public StaticBitmapImageBitmaptobitmapimage(Bitmap Bitmap) {using(MemoryStream stream =NewMemoryStream ()) {bitmap. Save (stream, imageformat.bmp); Stream. Position =0; BitmapImage result =NewBitmapImage (); Result. BeginInit ();//According to MSDN, "The default OnDemand caches option retains access to the stream until the image is needed." //force the bitmap-load right now so we can dispose the stream.Result. cacheoption = Bitmapcacheoption.onload; Result. Streamsource = stream; Result. EndInit (); Result. Freeze ();returnResult } }}
The effect after operation is as follows:
Add: A description of the location and area of the clipping, such as.
Cutting/transduction/cropping pictures of "C#/WPF" pictures