How to:  Convert System.IO.FileStream to a MemoryStream?

Tom Knowlton
Tom Knowlton used Ask the Experts™
System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'System.IO.FileStream' to type 'System.IO.MemoryStream'."
       at TestPixToolsLicensing.Form1.button1_Click(Object sender, EventArgs e) in c:\TestPixToolsLicensing\TestPixToolsLicensing\Form1.cs:line 41
       at System.Windows.Forms.Control.OnClick(EventArgs e)



//////////////////// DEBUGGER STOPS HERE //////////////////////////
MemoryStream ms = (MemoryStream)s;

Apparently it thinks my Stream variable is of type System.IO.FileStream.

So how do I convert a System.IO.FileStream to a MemoryStream?  My cast does not seem to do the trick.

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PixTools;
using PixTools.PixImaging;

namespace TestPixToolsLicensing
    public static class PixImageExtensions
        public static PixImage FromStream<T>(T stream) where T : Stream
            return new PixImage(String.Empty, 0, new StreamRIO<T>(stream));

        public static void ToStream<T>(PixImage image, T stream, PixFileType fileType,
                                      PixCompressionSettings settings) where T : Stream
            PixImageStorage.Save(image, fileType, String.Empty, settings,
                              OpenFileMode.CreateAlways, new StreamRIO<T>(stream));

    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                System.IO.StreamReader sr = new
                Stream s = sr.BaseStream;

//////////////////// STOPS HERE //////////////////////////
                MemoryStream ms = (MemoryStream)s;

                MemoryStream newms = Annotate(ms, "HIIIII", new Font(FontFamily.Families[0], 35f));

                pictureBox1.Image = new Bitmap(s);

                pictureBox2.Image = new Bitmap(newms);

        public MemoryStream Annotate(MemoryStream InputImage, string AnnotationText, Font AnnotationFont)
            //image from client
            PixImage pImageFromClient = PixImageExtensions.FromStream(InputImage);

            PixTools.Annotation.AnnotationDocument annotationDoc = new PixTools.Annotation.AnnotationDocument();

            PixTools.Annotation.TextAnnotation textItem = new PixTools.Annotation.TextAnnotation();

            textItem.Text = new PixTools.Annotation.ItemText(AnnotationText);

            textItem.Width = 300;
            textItem.BackColor = System.Drawing.Color.White;
            System.Drawing.Point sdp = new Point(10, 0);
            textItem.Location = sdp;

            textItem.Height = 40;

            PixImage newAnnotatedImage = pImageFromClient.MergeAnnotations(annotationDoc, MergeAttributes.MergeColorFormatPreserve, null);
            MemoryStream ms = new MemoryStream();
            PixImageExtensions.ToStream(newAnnotatedImage, ms, PixFileType.Tiff, PixCompressionSettings.Lzw);
            ms.Position = 0;
            return ms;


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
var fs = new FileStream(string.Concat(Directory.GetCurrentDirectory(), @"\test.txt"), FileMode.Open);
            var sr = new BinaryReader(fs);

            var ms = new MemoryStream(sr.ReadBytes((int) fs.Length));

Open in new window

Tom KnowltonWeb developer


How do you get around the fact that Length is a long?

How do you avoid possible truncation?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial