Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
how do i save an image in this way
from c# to sql db

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



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
Posted
Updated 25-Feb-12 6:53am
v2

hi you should convert your image to byte and then save at db
 
Share this answer
 
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:
C#
MemoryStream ms = new MemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();


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...
 
Share this answer
 
Comments
mikeoabban 25-Feb-12 12:16pm    
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
mikeoabban 25-Feb-12 13:02pm    
i added using System.IO; and the error was gone
thankx
OriginalGriff 26-Feb-12 3:24am    
Perfect!
Sergey Alexandrovich Kryukov 25-Feb-12 22:38pm    
Agree, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900