日期:2014-05-18  浏览次数:21023 次

WPF base64string转化为图片出现参数无效错误
如题,有遇到过这类问题的,看下如何解决,谢谢!

代码如下:
C# code


            string base64Str = e.Parameter.ToString();
            byte[] arr = Convert.FromBase64String(base64Str);
            using (MemoryStream ms = new MemoryStream(arr))
            {
                ms.Seek(0, SeekOrigin.Begin);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                Bitmap m = new Bitmap(img);
                IntPtr hBitmap = m.GetHbitmap();
                BitmapSizeOptions bso = BitmapSizeOptions.FromEmptyOptions();
                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, bso);
                Clipboard.SetImage(bs);
            }




------解决方案--------------------
我都是直接创建Bitmap对象,没有用到Image来转换,也没遇到过Lz所说的错误情况

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);

IntPtr hBitmap = bmp.GetHbitmap();

System.Windows.Media.Imaging.BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
------解决方案--------------------
这句就错了:string base64Str = e.Parameter.ToString();
如果你的e.Parameter本身就是String类型的, 哪来的ToString?如果你的e.Parameter是byte[],那么ToString的结果就是“System.Byte[]”
------解决方案--------------------
打断点看看base64Str里存的是什么东西啊。