将图像从C#格式保存到sql 2000 db [英] save an image from c# form to sql 2000 db

查看:61
本文介绍了将图像从C#格式保存到sql 2000 db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何以这种方式保存图像
从C#到sql db

how do i save an image in this way
from c# to sql db

public object myPicture
        {
            get { return picture; }
            set { picture = value.ToString(); }
        }




请帮助
这给了我一个错误

我尝试使用MemoryStream,但出现错误消息

找不到类型或名称空间名称"MemoryStream"(是否缺少using指令或程序集引用?)

请问我该怎么办




pls help
this is given me an error

i try using MemoryStream but had an error message

The type or namespace name ''MemoryStream'' could not be found (are you missing a using directive or an assembly reference?)

pls what should i do

推荐答案

hi 您应该将图像转换为字节,然后保存在db
hi you should convert your image to byte and then save at db


ToString不一定会执行您认为的操作:在很多情况下(并且Image就是其中一种),它返回类的名称:例如"System.Drawing.Bitmap".

如果要将图像存储到数据库中,则必须先将其转换为字节数组:
ToString does not necessarily do what you think it does: in many, many cases (and Images are one of those) it returns the name of the class: "System.Drawing.Bitmap" for example.

If you want to store images into a database, you have to convert them to an array of bytes first:
MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();



顺便说一句:将object类用作属性是一个非常非常愚蠢的想法-它消除了强类型输入的任何可能性,从而消除了在编译时而不是运行时捕获错误的可能性.如果要使用Image,请使用Image类,而不要使用Object.否则,每次使用它时,也都必须将其投射到图像上...



BTW: It is a very, very silly idea to use the object class as a property - it removes any chance of strong typing and thus errors being caught at compile time instead of run time. It you want an Image, use the Image class, not Object. Otherwise you also have to cast it to an Image every time you want to use it...


这篇关于将图像从C#格式保存到sql 2000 db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆