0

Possible Duplicate:
Converting .EPS to Image in C#

How to convert byte array to .eps image in C#?

I have a code which works with graphic images(.jpg, .png...) but it throws an argument exception when I'm converting to .eps format.

MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length);

using (ms)
{
      //saving image on current project directory
      Image img = Image.FromStream(ms);

      img.Save(Environment.CurrentDirectory + "file.eps");
}
Community
  • 1
  • 1
reederz
  • 983
  • 1
  • 12
  • 29

2 Answers2

2

If you just need to save it you can save it using the class File:

byte[] buffer  = ms.GetBuffer();
File.WriteAllBytes(Environment.CurrentDirectory + "file.eps", buffer);
Omar
  • 16,329
  • 10
  • 48
  • 66
  • I've got an unauthorized access exception. It may be because the byte array I'm working with is in database. I am just guessing – reederz Apr 29 '12 at 18:33
  • You can try to use directly `byteArray`, if you don't need the memorystream. – Omar Apr 29 '12 at 18:39
1

From the code you've posted it seems that you only save an image to a file with *.eps extension. If it's so, you don't have to create a MemoryStream object at all, just use this method:

try
{
    File.WriteAllBytes(Environment.CurrentDirectory + "file.eps", byteArray);
}
catch (Exception err)
{
    //your exception handling code here
}

It should work for all types of files, but without verifying if the file content's is a valid image. However, if you are sure what the file's contents are, it should be good method to use in this case. This method, however, can throw a number of exceptions, so be sure to handle them appropriately.

Omar
  • 16,329
  • 10
  • 48
  • 66
Lukasz M
  • 5,635
  • 2
  • 22
  • 29