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

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'System.IO.FileStream' to type 'System.IO.MemoryStream'."
  Source="TestPixToolsLicensing"
  StackTrace:
       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)

etc.

etc.



//////////////////// 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()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                   System.IO.StreamReader(openFileDialog1.FileName);
                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);
                sr.Close();
            }
        }

        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;


            annotationDoc.Items.Add(textItem);
            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

Comment
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));
            sr.Close();
            fs.Close();

Open in new window

Tom KnowltonWeb developer

Author

Commented:
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