Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

Need Memory Stream for Saving Bitmaps

I am using the code below, which is actually called numerous times within an instantiated object, and therefore there should not be any conflict since its in its own object, however, I think the IO is bottlenecking(?)

The exception returned from the fist line is a GDI+ error.  It's obvious a memory stream should be used the same way instantiating a streamreader and streamwriter prevent conflicts.   At present, I can't seem to put anything together correctly that saves to the memorystream, and then writes the memorystrem to disk.  

 
        bmp.Save(sI, ImageDefaultFmt)
        bmp.Dispose()
        gr.Dispose()

Open in new window

0
lep1
Asked:
lep1
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Jacques Bourgeois (James Burger)Commented:
Not enough code to understand.

But you state that this is called numerous times within an instantiated object. If that object is either bmp or gr, then it's normal that it crashed, because Dispose probably cleaned up stuff that is important for that object to work.
0
 
lep1Author Commented:
Tried the code below, but get a null parameter exception on the bmp.save line.
        Dim ms As New MemoryStream
        bmp.Save(ms, ImageDefaultFmt)  'Null parameter exception
        Dim fs As FileStream = File.OpenWrite(sI)
        ms.WriteTo(fs)
        ms.Close()
        fs.Close()
    

Open in new window

0
 
Jacques Bourgeois (James Burger)Commented:
Is this the complete code? If you removed lines, thinking that it would be simpler, the problem might be in these removed lines.

What type of object is bmp? A Bitmap?

Have you checked the content of ImageDefaultFmt?

You have not set the length of the MemoryStream. You need to set it either in the constructor, or by using SetLength. With a Bitmap, you might use bmp.Size as a value, plus maybe one or 2 bytes just to be sure that the buffer is set high enough.

Why go through a MemoryStream when what you want is to write in a FileStream?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
ktaczalaCommented:
sI in the first code list should point to a filename i.e. c:\temp\mypicture
and imagedefaultfmt should be a valid extension.

In the second code list you have ms as a memorystream,  not a filename

for testing sometimes you need to hard code the variables.
before you run your section of code.
sI = "c:\temp\mypicture.jpg" ' <<< what ever path and name you want to test with.
imagedefaultfmt = "Imaging.ImageFormat.Jpeg" ' <<< which ever format you want
then see what happens
0
 
lep1Author Commented:
Thanks, and yes, sI is the filename.  The code below is close, but hangs on the bmp.save line:

 Dim fs As New FileStream(sI, FileMode.Create, FileAccess.Write)
        bmp.Save(fs, ImageDefaultFmt.) 'System.Drawing.Imaging.ImageFormat.Jpeg)
        fs.Close()
        bmp.Dispose()

Open in new window


In Form 1 preamble, I am defining ImageDefaultFMt as an image format:

Dim ImageDefaultFmt As ImageFormat

Open in new window


and then setting in the Load method of Form1:

 ImageDefaultFmt = ImageFormat.Png

Open in new window

     

But in the bmp.save above, the ImageDefaultFmt looks like it has all the values of formats - so do I neet to Cast in order to get the .png out of it?

The code works if I use the commented-out code to the right.   Question is, how can I carry around a wild-card for image format, and then fetch it from ImageDefaultFmt?
0
 
Naman GoelCommented:
You need to Initialize memorystream before using it:

Bitmap bitmap = Image.FromFile(@"e:\test.jpg", true) as Bitmap;
            ImageConverter converter = new ImageConverter();
            byte[] bytes = converter.ConvertTo(bitmap, typeof(byte[])) as byte[];
            MemoryStream ms= new MemoryStream(bytes);
            bitmap.Save(ms, ImageFormat.Jpeg);
            using (FileStream file = new FileStream(@"e:\test1.jpg", FileMode.Create, System.IO.FileAccess.Write))
            {
                file.Write(bytes, 0, bytes.Length);
                ms.Close();
            }
            ms.Dispose();

Open in new window


or

 Bitmap bitmap = Image.FromFile(@"e:\test.jpg", true) as Bitmap;
            byte[] bytes = null;
            MemoryStream ms= new MemoryStream(100);
            bitmap.Save(ms, ImageFormat.Bmp);
            
            ms.Close();
            using (FileStream file = new FileStream(@"e:\test1.bmp", FileMode.Create, System.IO.FileAccess.Write))
            {
                bytes = ms.ToArray();    
                file.Write(bytes, 0, (int)bytes.Length);
                ms.Close();
            }
            ms.Dispose();

Open in new window

0
 
Naman GoelCommented:
and here is code for getting default ImageFormat of Bitmap:

public static System.Drawing.Imaging.ImageFormat GetImageFormat(this System.Drawing.Image img)
    {             
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            return System.Drawing.Imaging.ImageFormat.Jpeg;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            return System.Drawing.Imaging.ImageFormat.Bmp;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            return System.Drawing.Imaging.ImageFormat.Png;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
            return System.Drawing.Imaging.ImageFormat.Emf;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))
            return System.Drawing.Imaging.ImageFormat.Exif;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
            return System.Drawing.Imaging.ImageFormat.Gif;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
            return System.Drawing.Imaging.ImageFormat.Icon;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))
            return System.Drawing.Imaging.ImageFormat.MemoryBmp;
        if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
            return System.Drawing.Imaging.ImageFormat.Tiff;
        else
            return System.Drawing.Imaging.ImageFormat.Wmf;            
    }

Open in new window


http://stackoverflow.com/questions/1397512/find-image-format-using-bitmap-object-in-c-sharp
0
 
ktaczalaCommented:
dim imagefmt as ".jpg"         ' define and set this in somewhere your code
   Select Case imagefmt
                Case ".jpg"
                    img.Save(FileName & imagefmt, Imaging.ImageFormat.Jpeg)
                Case ".bmp"
                    img.Save(FileName & imagefmt, Imaging.ImageFormat.Bmp)
                Case ".tif"
                    img.Save(FileName & imagefmt, Imaging.ImageFormat.Tiff)
                Case ".png"
                    img.Save(FileName & imagefmt, Imaging.ImageFormat.Png)
            End Select
make sure your filename includes the correct extension, it's not added by default.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now