BitmapImage result =newBitmapImage(); result.BeginInit();// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."// Force the bitmap to load right now so we can dispose the stream.result.CacheOption = BitmapCacheOption.OnLoad; res...
bitmapImage.CacheOption=BitmapCacheOption.OnLoad; bitmapImage.UriSource=newUri(szPath);//szPath为图片的全路径bitmapImage.EndInit(); bitmapImage.Freeze(); image1.Source= bitmapImage; 1. 2. 3. 4. 5. 6. 7. 二、Bitmap转BitmapImage 先将Bitmap储存成memorystream,然后指定给BitmapImage priv...
bitmap) { //Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { ...
///从Bitmap转换成BitmapSource///</summary>///<param name="bmp"></param>///<returns></returns>publicstaticBitmapSource ChangeBitmapToBitmapSource(thisBitmap bmp) { BitmapSource returnSource;try{ returnSource=Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect....
public BitmapSource Convert(System.Drawing.Bitmap bitmap){ var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap....
bitmapImage.EndInit(); return bitmapImage; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. B: [DllImport("gdi32")] private static extern int DeleteObject(IntPtr o); public ImageSource ToBitmapSourceB(Bitmap bitmap) { IntPtr ptr = bitmap.GetHbitmap(); //obtain the Hbitmap ...
BitmapImage as a source for other BitmapSource objects.///BitmapImage myBitmapImage =newBitmapImage();// BitmapSource objects like BitmapImage can only have their properties// changed within a BeginInit/EndInit block.myBitmapImage.BeginInit(); myBitmapImage.UriSource =newUri(@"sampleImages...
在WPF(Windows Presentation Foundation)中,Bitmap 和ImageSource 是两种常用的图像表示形式。Bitmap 通常用于表示像素数据,而 ImageSource 是WPF 中用于图像绘制的更高级别的抽象。要将 Bitmap 转换为 ImageSource,你可以使用 BitmapImage 类,它继承自 ImageSource 并提供了加载位图图像的功能。 以下是如何在 WPF 中...
BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); ms.Dispose(); img1.Source = bitmapImage; 好,看完了一般情況的使用之後,來看一下System.Drawing.Bitma...
bitmapImage.EndInit(); bitmapImage.Freeze();this.picImage.Source =bitmapImage; }///<summary>///转换Bitmap到BitmapSource(经本人测试此方法为效率最高,内存最低)///</summary>///<param name="bmp"></param>///<returns></returns>publicvoidGetBitmapSource(System.Drawing.Bitmap bmp) ...