Solved

Need Memory Stream for Saving Bitmaps

Posted on 2014-02-01
8
647 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:lep1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now