有一些库的设计是需要传入一个 BitmapImage 图片,但是我需要从界面代码创建图片,我没有文件,如何通过 DrawingVisual 画出的控件转换 BitmapImage 传给库? 需要将 DrawingVisual 转为 RenderTargetBitmap 然后将 RenderTargetBitmap 转为 BitmapImage 才可以 先创建一个 DrawingVisual DrawingVisual 转 RenderTargetBitmap 就可以通过 PngBitmapEncoder 将 RenderTargetBitmap 转图片 var bitmapImage (); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } 通过这个方法就可以将 DrawingVisual 转 BitmapImage
有一些库的设计是需要传入一个 BitmapImage 图片,但是我需要从界面代码创建图片,我没有文件,如何通过 DrawingVisual 画出的控件转换 BitmapImage 传给库? 需要将 DrawingVisual 转为 RenderTargetBitmap 然后将 RenderTargetBitmap 转为 BitmapImage 才可以 先创建一个 DrawingVisual DrawingVisual 转 RenderTargetBitmap 就可以通过 PngBitmapEncoder 将 RenderTargetBitmap 转图片 var bitmapImage (); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } 通过这个方法就可以将 DrawingVisual 转 BitmapImage
问题解决了 先将BitmapImage转化为WriteableBitmap,然后得到base64字符串,然后可以得到base64的byte[]数组,再然后您可以把byte[]变成Stream 关键代码: //这里的b为上面生成的base64编码的byte数组 2 MemoryStream ms = new MemoryStream(b); 3 BitmapImage bitImage = new BitmapImage
bitmapImage = new BitmapImage(); InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream bitmapImage = new BitmapImage(); InMemoryRandomAccessStream randomAccessStream = new bitmapImage = new BitmapImage(); InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream var thumbnail = await GetThumbnailAsync(File, n); BitmapImage bitmapImage = new BitmapImage bitmapImage = new BitmapImage(); InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream
bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = mem; bmp.EndInit(); imgOutput.Source = bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = mem; bmp.EndInit 亮度—变暗 代码如下: BitmapImage bmpSource = new BitmapImage(new Uri("pack://application:,,,/images/ocv02.jpg" 我们通过BitmapImage导入图片后,直接使用BitmapImage的对象的ToMat方法,把BitmapImage转换成了Mat类的对象。 这个ToMat方法,我们需要特别留意一下,因为他并不是BitmapImage类的方法,它是一个扩展方法,只有我们引用了OpenCvSharp命名空间,BitmapImage和Bitmap对象才会增加扩展方法
FileStream 创建文件的方法 如果直接通过文件的 URL 创建,那么可能出现文件被占用的问题,不能比较好做文件的修改,建议通过内存的方式加载 下面是通过内存加载的代码 var bitmapImage = new BitmapImage(); using (var fileStream = new FileStream("文件路径", FileMode.Open)) (); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); 如果调用了 memoryStream.Dispose 就会显示空白而不是图片 var bitmapImage = new BitmapImage(); using (); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit();
= new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual 将两个图片同时画出来 public MainWindow() { InitializeComponent(); var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual dc.DrawImage(croppedBitmap, new Rect(10, 10, 50, 50)); dc.DrawImage(bitmapImage = new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual
= new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual 将两个图片同时画出来 public MainWindow() { InitializeComponent(); var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual dc.DrawImage(croppedBitmap, new Rect(10, 10, 50, 50)); dc.DrawImage(bitmapImage = new BitmapImage(new Uri("pack://application:,,,/1.jpg")); var drawingVisual = new DrawingVisual
BitmapImage BitmapImage从图像文件创建位图,并将其用作 Image 控件的源 // Create the image element. BitmapImage bi = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block. bi.BeginInit 相互转换 // Bitmap --> BitmapImage public static BitmapImage BitmapToBitmapImage(Bitmap bitmap) { using --> Bitmap public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage) { using (MemoryStream (RenderTargetBitmap wbm) { BitmapImage bmp = new BitmapImage(); using (MemoryStream stream = new
我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说。 主要说的是 BitmapImage 和 WriteableBitmap 、二进制 byte 的互转。 ---- 2017年1月4日21:45:37 ---- 我后来过了几个月,发现我们的 BitmapImage 可以转 byte[] 我们可以通过拿 BitmapImage 的 UriSource 把它转为 WriteableBitmap ,可以使用截图获得 BitmapImage。 那么我们需要首先在 Image 使用 Tap ,假如图片 source 是 BitmapImage 前提安装 WriteableBitmapEx ,假如我们的 ViewModel有一个 BitmapImage
bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad ; bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径 bitmapImage.EndInit(); bitmapImage.Freeze() ; image1.Source = bitmapImage; Bitmap => BitmapImage 先将Bitmap储存成memorystream,然后指定给BitmapImage private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap) { BitmapImage bitmapImage = new BitmapImage
PropertyChanged(this, new PropertyChangedEventArgs("Status")); } } private BitmapImage _imageSource = new BitmapImage(); public BitmapImage ImageSource { get { return result.HasValue || result.Value == false) return; BitmapImage imageSource = new BitmapImage(); try { imageSource.SetSource(ofd.File.OpenRead Exception) { model.Status = "加载失败"; } } 提示: 不要忘记引入“BitmapImage
GetImage(string imagePath) { BitmapImage bi = new BitmapImage(); if GetImage(string imagePath) { BitmapImage bi = new BitmapImage(); if Bitmap2BitmapImage(Bitmap bitmap) { BitmapImage bitmapImage = new BitmapImage(); ; bitmapImage.EndInit(); } return bitmapImage; } = ByteArray2Bitmap(bytes)) { BitmapImage bitmapImage = Bitmap2BitmapImage
FileStream 创建文件的方法 如果直接通过文件的 URL 创建,那么可能出现文件被占用的问题,不能比较好做文件的修改,建议通过内存的方式加载 下面是通过内存加载的代码 var bitmapImage = new BitmapImage(); using (var fileStream = new FileStream("文件路径", FileMode.Open)) (); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); 如果调用了 memoryStream.Dispose 就会显示空白而不是图片 var bitmapImage = new BitmapImage(); using (); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit();
wpf使用gdi 定义属性成员 System.Drawing.Image _bitmap; public BitmapImage BitmapSource { get { MemoryStream _ms = new MemoryStream(); _bitmap.Save(_ms, ImageFormat.Png); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = _ms; bitmapImage.EndInit(); return bitmapImage; } } 调用gdi+绘制,10000个1-2s,10w个3-4s
= new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = stream ; bitmapImage.EndInit(); 使用 BitmapImage 解析文件 创建图片 在读取图片之后就可以创建图片 var writeableBitmap = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = stream ; bitmapImage.EndInit(); var formatConvertedBitmap = new FormatConvertedBitmap = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = stream
sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { BitmapImage bi = new BitmapImage(); bi.SetSource(e.Result); xImage.Source = bi HtmlPage.Document.DocumentUri, MediaUrl); ImageSource img = new System.Windows.Media.Imaging.BitmapImage
这里图片我用BitmapImage,Uri是输入网络的 下载图片 图片也是和其他一样,我们可以简单用系统给的网络web下载。 我们需要输入Uri,然后把图片下载。 img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream img = new BitmapImage(); await img.SetSourceAsync(stream); return img = new BitmapImage(); await img.SetSourceAsync(stream); img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream
本文即将采用以下四种方法获取图片: System.Drawing.Imaging.Metafile System.Drawing.Bitmap System.Windows.Media.Imaging.BitmapImage var bitmap = new BitmapImage(new Uri(@"D:\blog.walterlv.com\large-background-image.jpg", UriKind.Absolute System.Windows.Media.Imaging.BitmapDecoder 这也是 WPF 框架中提供的方法,但相比完全加载图片到可以显示的 System.Windows.Media.Imaging.BitmapImage 做成图表,对于同一张图片运行不同次数: 消耗时间(ms) Metafile Bitmap BitmapImage BitmapDecoder 1次 175 107 71 2 10次 1041 1046 对于不同图片运行不同次数: 消耗时间(ms) Metafile Bitmap BitmapImage BitmapDecoder 1次 175 107 71 2 10次 998 980 83 20 100
这里图片我用BitmapImage,Uri是输入网络的 下载图片 图片也是和其他一样,我们可以简单用系统给的网络web下载。 我们需要输入Uri,然后把图片下载。 img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream img = new BitmapImage(); await img.SetSourceAsync(stream); return img = new BitmapImage(); await img.SetSourceAsync(stream); 如果图片在缓存里,直接返回图片 var bitmapImage = await ImageCache.Instance.GetFromCacheAsync(distantUri); // 清理缓存 await