Bitmap and BitmapImage transformations in WPF (C #)

Source: Internet
Author: User

One, the WPF image control is set in ImageSource

new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));

You can also use:

System. IO. FileStreamFS = new System. IO. FileStream(FilePath, System. IO. FileMode. Open, the System. IO. FileAccess. Read);byte[] buffer = new BYTE[FS. Length]; fs. Read (buffer, 0, buffer. Length);Fs. Close(); fs. Dispose ();System. IO. MemoryStreamms = new System. IO. MemoryStream(buffer);BitmapImage BitmapImage = new BitmapImage ();BitmapImage. BeginInit();BitmapImage. Streamsource= ms;BitmapImage. Cacheoption= BitmapCacheOption. OnLoad; Bitmapimage.endinit ();Ms. Dispose();Image1. Source= BitmapImage;

You can also use:

BitmapImage bitmapImage = new BitmapImage();bitmapImage.BeginInit();bitmapImage.CacheOption = BitmapCacheOption.OnLoad;bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径bitmapImage.EndInit();bitmapImage.Freeze();image1.Source = bitmapImage;

Second, bitmap turn bitmapimage
Save the bitmap as MemoryStream and assign it to BitmapImage

Private BitmapImage Bitmaptobitmapimage (System. Drawing. BitmapBitmap) {BitmapImage BitmapImage = new BitmapImage ();using (System. IO. MemoryStreamms = new System. IO. MemoryStream()) {bitmap. Save(MS, bitmap. Rawformat);BitmapImage. BeginInit();BitmapImage. Streamsource= ms;BitmapImage. Cacheoption= BitmapCacheOption. OnLoad;BitmapImage. EndInit();BitmapImage. Freeze();}return BitmapImage;}image1. Source= Bitmaptobitmapimage (bitmap);

Third, bitmap turn BitmapSource

/ *-------------------------------------------------------------------------//imaging.createbitmapsourcefromhbitmap method, A pointer based on the provided unmanaged bitmap and palette information,// Returns a managed bitmapsource---------------------------------------------------------------------------* /Bitmap Bitmap = Capturescreen. Getdesktopimage();IntPtr IP = bitmap. Gethbitmap();//Create GDI bitmap objects from GDI + bitmapBitmapSource BitmapSource = System. Windows. Interop. Imaging. Createbitmapsourcefromhbitmap(IP, INTPTR. Zero, Int32Rect. Empty, the System. Windows. Media. Imaging. Bitmapsizeoptions. Fromemptyoptions());Image1. Source= BitmapSource;

Four, BitmapSource turn bitmap

BitmapSource m = (bitmapsource) image1. Source;System. Drawing. BitmapBMP = New System. Drawing. Bitmap(M. PixelwidthM. Pixelheight, the System. Drawing. Imaging. PixelFormat. Format32BPPPARGB); System. Drawing. Imaging. BitmapDatadata = BMP. LockBits(New System. Drawing. Rectangle(System. Drawing. Point. EmptyBmp. Size), System. Drawing. Imaging. ImageLockMode. WriteOnly, the System. Drawing. Imaging. PixelFormat. Format32BPPPARGB); M. CopyPixels(Int32Rect. Empty, the data. Scan0, data. Height* Data. Stride, the data. Stride); bmp. Unlockbits (data);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bitmap and BitmapImage transformations in WPF (C #)

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.