Stream to PDF creates a corrupt or damaged file

Ayar K 1 Reputation point
2022-10-24T20:26:07.427+00:00

I am creating PDF files from stream:

using (Stream file = File.Create($"C:\test.pdf"))
{
CopyStream(myStream, file);
}
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}

The files created, but when i can't open it with any program, i get the message that file is not valid, or is corrupted.

What i am doing wrong here?

Thanks in advance

Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.