当前位置: 首页 > 编程语言 > C#

wpf-我可以在wpf中使用System.Drawing吗?

时间:2023-04-10 20:57:42 C#

wpf-我可以在wpf中使用System.Drawing吗?我将图像保存在数据库中。..但是如何从数据库中检索该图像..当我尝试使用system.drawing..它显示错误..有些人说我不能在wpf中使用system.drwa..甚至dll文件..我的代码是私有的voidbtnShow_Click(objectsender,RoutedEventArgse){DataTabledt2=reqBll.SelectImage().Tables[0];byte[]data=(byte[])dt2.Rows[0][1];MemoryStreamstrm=newMemoryStream();strm.Write(data,0,data.Length);strm.Position=0;System.Drawing.Imageimg=System.Drawing.Image.FromStream(strm);BitmapImagebi=newBitmapImage();双。开始初始化();MemoryStreamms=newMemoryStream();img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);ms.Seek(0,SeekOrigin.Begin);bi.StreamSource=ms;bi.EndInit();ImgBox.Source=bi;}现在做什么?我使用了system.drawing.dll..现在我可以使用system.drawing.bitmap..但是在使用它之后它在System.Drawing.Image.FromStream(strm)中显示错误;错误:-用户代码未处理参数异常参数无效。您可以使用System.Drawing命名空间中的类,但您必须通过右键单击项目并选择“添加引用...”选项来添加对包含您感兴趣的类的程序集的引用。就绘图部分而言,您的代码很好,问题可能出在您尝试从数据库加载的图像数据上(可能是由于数据格式不匹配或选择了错误的列等引起的)。您可能想要共享将图像保存到数据库的代码,因为没有它就无法知道。此代码示例执行您想要的操作(我注释掉了数据库相关部分并将其替换为文件加载):privatevoidbtnShow_Click(objectsender,RoutedEventArgse){//DataTabledt2=reqBll.SelectImage().Tables[0];//byte[]data=(byte[])dt2.Rows[0][1];//内存流strm=new内存流();//strm.Write(data,0,data.Length);System.Drawing.Imagebmp=System.Drawing.Bitmap.FromFile(@"C:Temptest.png");内存流strm=新内存流();bmp.Save(strm,System.Drawing.Imaging.ImageFormat.Bmp);strm.Position=0;System.Drawing.Imageimg=System.Drawing.Image.FromStream(strm);BitmapImagebi=newBitmapImage();bi.BeginInit();MemoryStreamms=newMemoryStream();img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);ms.Seek(0,SeekOrigin.Begin);bi.StreamSource=ms;bi.EndInit();imgBox.Source=bi;话虽如此,如果这是一个新的应用程序,单独使用WPF比混合使用Windows窗体和WPF类和元素要好(正如JeffMercado指出的那样)。以上是C#学习教程:wpf-CanIuseSystem.Drawinginwpf?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: