Quantcast
Channel: Ghostscript.NET
Viewing all articles
Browse latest Browse all 393

New Post: problem with GhostscriptRasterizer reading from MemoryStream and the solution

$
0
0
Just to keep others up-to-date.

With help from Chanan this problem is fixed. Now EPS files with the preview header signature are also supported.

Untill next release (1.1.3) here is the code that has to be replaced in Ghostscript.NET->Viewer->GhostscriptViewer.cs:
#region StoreMemoryStreamToTemporaryFile

private string StoreMemoryStreamToTemporaryFile(MemoryStream stream)
{
    if (stream.Length < 4)
    {
        throw new InvalidDataException();
    }

    byte[] test = new byte[4];

    stream.Read(test, 0, 4);

    stream.Position = 0;

    string extension = string.Empty;

    if (test[0] == 0x25 && test[1] == 0x21) // standard ps or eps signature
    {
        extension = ".ps";

        if (stream.Length > 23)
        {
            test = new byte[23];
            stream.Read(test, 0, 23);

            stream.Position = 0;

            string tmp = System.Text.Encoding.ASCII.GetString(test);

            if (tmp.ToUpper().Contains("EPS"))
            {
                extension = ".eps";
            }
        }
    }
    else if (test[0] == 0xc5 && test[1] == 0xd0 && test[2] == 0xd3 && test[3] == 0xc6) // eps with preview header signature / magic number (always C5D0D3C6)
    {
        extension = ".eps";
    }
    else if (test[0] == 0x25 && test[1] == 0x50 && test[2] == 0x44 && test[3] == 0x46) // pdf signature
    {
        extension = ".pdf";
    }
    else
    {
        throw new FormatException("Stream format is not valid! Please make sure it's PDF, PS or EPS.");
    }

    string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + extension);

    File.WriteAllBytes(path, stream.ToArray());

    _fileCleanupHelper.Add(path);

    return path;
}

#endregion

Viewing all articles
Browse latest Browse all 393

Trending Articles