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
Solved

Need Memory Stream for Saving Bitmaps

Posted on 2014-02-01
8
689 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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