当前位置: 代码迷 >> Silverlight >> WPF ImageBrush 变换
  详细解决方案

WPF ImageBrush 变换

热度:9119   发布时间:2013-02-26 00:00:00.0
WPF ImageBrush 转换
 GIF图片
 <ImageBrush x:Key="ImageLoadingBrush" >
        <ImageBrush.ImageSource>
            <BitmapImage UriSource="pack://Application:,,,/chatres;component/Resources/ImageLoading.gif" />
        </ImageBrush.ImageSource>
    </ImageBrush>

System.Windows.Media.ImageBrush imageBrush = this.Resources["ImageLoadingBrush"] as ImageBrush;
System.Windows.Media.ImageBrush如何转换成System.Drawing.Bitmap?或者Byte[]都可以

------解决方案--------------------------------------------------------

   var image = System.Drawing.Image.FromFile("..."); // or wherever it comes from
      var bitmap = new System.Drawing.Bitmap(image);
      var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                                                                            IntPtr.Zero,
                                                                            Int32Rect.Empty,
                                                                            BitmapSizeOptions.FromEmptyOptions()
            );
      bitmap.Dispose();
      var brush = new ImageBrush(bitmapSource);    

------解决方案--------------------------------------------------------
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.ImageBrush imageBrush = this.Resources["ImageLoadingBrush"] as ImageBrush;

            var bitmap = GetBitmap(((BitmapSource)imageBrush.ImageSource));

            bitmap.Save("E:\\茉莉花.jpg");
        }

        Bitmap GetBitmap(BitmapSource source)
        {
            Bitmap bmp = new Bitmap(source.PixelWidth,source.PixelHeight,PixelFormat.Format32bppPArgb);
  相关解决方案