Lucky:)

I"m using .net 3.5 and wpf.

Is there a way how to serialize/deserialize an image to string



Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Martin Platt

You would need to read the image data, as a stream of bytes. You could write that out as a string I guess, although I'd have to ask the question, why !

If you were going to do something with it, you might want to convert the stream to base 64, I could see the point to that.

What are you trying to do

I hope this helps,

Martin Platt.






Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Peter Ritchie

I agree with Martin. For example:

Code Snippet

// assumed you already have an image object;

// loading from file then serializing to memory before

// calling ToBase64String doesn't make much sense.

// You could simply read the file.

Image image = Image.FromFile(@"C:\image.gif");

string text;

using(MemoryStream memory = new MemoryStream())

{

image.Save(memory, System.Drawing.Imaging.ImageFormat.Gif);

text = System.Convert.ToBase64String(memory.ToArray());

}






Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Lucky:)

I need to save it in DB.





Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

xsdevnet

If you're storing a image in a database you should consider a blob or binary type instead of converting the image to and from text with each select and insert. It will save you processing time and actually use less space.





Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Martin Platt

For a database! Ah! That changes everything.

You definitely need to use a blob (binary large object) in the database.

Here are a couple of articles on how you might do it, one is in VB.NET, but you can read that and get ideas, can you not It also links to other articles that might be of use to you.

http://www.codeproject.com/cs/database/AccessBlob.asp

http://www.thescripts.com/forum/thread489246.html

I hope this helps you,

Good luck,

Martin Platt.






Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Lucky:)

Ok. It works for me.





Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Lucky:)

thanks, good sources.





Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Martin Platt

could you mark us as helpful then, please !

Smile

Martin Platt.






Re: Visual C# General How can I serialize/deserialize an image to string? (.NET 3.5)

Serguei Razykov

I am GETTIG an image as a MIME string, need to display it in a web page. Any ideas how to do this really appreciated