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 #)