您的位置:程序门 -> .net技术 -> c#



如何从sql数据库中获取图片


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何从sql数据库中获取图片[已结贴,结贴人:storm0912]
发表于:2007-04-02 08:16:43 楼主
我的应用程序是c/s结构的,数据库中某张表有一个image字段,现在我定义了一个结构体包含了这张表的结构,譬如:
class     car
{  
  string     price
  int           distance
  string     name
    .....
}  
如何在程序中建立相对于数据库中image字段的变量
    byte[]   picture
可以吗?
如果是这样,我怎样将已经显示在datagridview中的一条记录(其中包含image字段的内容)提取出来,存入我的变量中啊?
可能各位要考虑下序列化问题,因为我是要把服务器端的数据传入客户端。
发表于:2007-04-02 08:50:291楼 得分:0
顶下
发表于:2007-04-02 08:54:162楼 得分:10
你可以写一个如下的函数来得到相应的image
public   image   getimage(object   o)
{
image   img   =   null;
byte[]   bts   =   o   as   byte[];
if   (bts   !=   null)
{
memorystream   ms   =   null;
try
{
ms   =   new   memorystream(bts,   0,   bts.length);
{
img   =   image.fromstream(ms,   true);
}
}
catch   {   }
finally
{
if   (ms   !=   null)
{
ms.close();
}
}
}
return   img;
}
发表于:2007-04-02 08:56:043楼 得分:0
也可以用如下的方法,这个更安全些:
public   image   converttoimage(object   o)
{
if   (o   is   byte[])
{
imageconverter   imc   =   new   imageconverter();
image   _img   =   imc.convertfrom(null,   system.globalization.cultureinfo.currentculture,   o)   as   image;
if   (_img   !=   null)
{
rectangle   imgrect   =   getimgpaintrect(rect,   _img.width,   _img.height,   col.m_valueformat.alignment);
g.drawimage(_img,   imgrect);
_img.dispose();
_img   =   null;
return;
}
}
return   null;
}
发表于:2007-04-03 12:03:244楼 得分:0
那我又该如何将一幅jpg图片通过服务器传入到数据库中呢?
请赐教
发表于:2007-04-03 17:57:045楼 得分:0
顶下


快速检索

最新资讯
热门点击