C # converts RGB image format data to bitmap and Bitsource objects

Source: Internet
Author: User


1 Public voidRgbtobitmap (BYTE/*Custom Data IntPtr*/BitsintWidth/*the width of the image*/,intHeight/*the height of the image*/)2 intWidthstep = width*3;//The general default step is 3 times times the width3 intLendata = widthstep * height;//data length, length of 24-bit image data = width * Height4 intLen =0;//representative Length5 //bits image frame data6 byte[] buffer =New byte[Len];//creates a specified length of byte data7Marshal.Copy (Bits, buffer,0, buffer. Length);8 9 Ten /** * * * The following is the conversion of RGB data to a Bitmap object * * **/ One intStride = width *3; AGCHandle handle =gchandle.alloc (buffer, gchandletype.pinned); - intScan0 = (int) handle. Addrofpinnedobject (); -Scan0 + = (Height-1) *Stride; theSystem.Drawing.Bitmap Bitmap =NewSystem.Drawing.Bitmap (width, height,-Stride, SYSTEM.DRAWING.IMAGING.PIXELFORMAT.FORMAT24BPPRGB, (INTPTR) scan0); - handle. Free (); - /********************************************/ - + /** * * * The following code is displayed in real time on the image control of WPF * * * * **/ -IntPtr IP = bitmap. Gethbitmap ();//To create a GDI bitmap object from GDI + bitmap + ABitmapSource BitmapSource =System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap (IP, IntPtr.Zero, Int32rect.empty, at System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions ()); - -Image1. Source =BitmapSource; - /********************************************/ -Bitmap. Dispose ();//Bitmap Release - in /*using DELETEOBEJCT This function needs to import functions from Gdi32.dll - [System.Runtime.InteropServices.DllImport ("Gdi32.dll")] to Public static extern bool DeleteObject (IntPtr hobject);*/ +DeleteObject (IP);

C # converts RGB image format data to bitmap and Bitsource objects

Related Article

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.