Cutting/transduction/cropping pictures of "C#/WPF" pictures

Source: Internet
Author: User

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

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.