简体   繁体   中英

Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish

I am saving Image in a database. It works great when I Debug it. , but after publish it and I try to load or try to save the picture it gives me type cast error. I am using following code to save image:

MemoryStream ms = new MemoryStream();
kephelye.Image.Save(ms, kephelye.Image.RawFormat);
byte[] img = ms.ToArray();

and load image:

DataSet ds = new DataSet();
da.Fill(ds);
byte[] ap = (byte[])(ds.Tables[0].Rows[0]["kepcim"]);
MemoryStream ms = new MemoryStream(ap);
if (ms.Length != 0) {
 kephelye2.Image = Image.FromStream(ms);
}

ms.Close();

Simply get the image from the memory stream into a new bitmap, and close the stream.

public static Image GetImage(byte[] buffer, int offset, int count)
{
    var memoryStream = new MemoryStream(buffer, offset, count);
    Bitmap bm = new Bitmap(Image.FromStream(memoryStream));
    // Close the stream here, won't affect the bitmap.
    return bm;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM