Solved

Need Memory Stream for Saving Bitmaps

Posted on 2014-02-01
8
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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
 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 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