Solved

Need Memory Stream for Saving Bitmaps

Posted on 2014-02-01
8
664 Views
Last Modified: 2014-02-01
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
Comment
Question by:lep1
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 40
ID: 39826658
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
 

Author Comment

by:lep1
ID: 39826663
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
 
LVL 40
ID: 39826697
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
 
LVL 12

Expert Comment

by:ktaczala
ID: 39826720
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:lep1
ID: 39826749
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39826761
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39826770
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
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
ID: 39826773
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now