Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / December 2006

Tip: Looking for answers? Try searching our database.

Still playing with bitmap images

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Earl - 02 Dec 2006 07:21 GMT
I have an image issue that I do not understand. When I try to save a bitmap
created from a picturebox image, I can save without exception so long as the
bitmap was retrieved from a file and loaded into the picturebox. But if I
load the image from the database into the picturebox and try to save
(without change), I then get a null exception telling me that the encoder
parameter is null. I'm speculating that the file provides the encoder param
but the database image does not. What do I need to know to resolve this?

int intWidth = pb.Width;
int intHeight = pb.Height;
Bitmap bmp = new Bitmap(pb.Image, intWidth, intHeight);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, pb.Image.RawFormat);
arrayImage = ms.GetBuffer();
ms.Close();
Kevin Spencer - 02 Dec 2006 14:59 GMT
You should find the following resource very helpful:

http://www.bobpowell.net/gdiplus_faq.htm

Signature

HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.

>I have an image issue that I do not understand. When I try to save a bitmap
>created from a picturebox image, I can save without exception so long as
[quoted text clipped - 11 lines]
> arrayImage = ms.GetBuffer();
> ms.Close();
Earl - 02 Dec 2006 17:41 GMT
Thanks Kevin, I'd already read most of Bob's stuff and I didn't find
anything directly on point.

> You should find the following resource very helpful:
>
[quoted text clipped - 16 lines]
>> arrayImage = ms.GetBuffer();
>> ms.Close();
Kevin Spencer - 03 Dec 2006 00:01 GMT
Hi Earl,

Sorry about that. The answer is there, but not directly. It's in an article
about saving JPEGs with a specific compression
(http://www.bobpowell.net/jpeg_compression.htm).  The EncoderParameters
array is included in 2 overloads of the base Image class Save method, in
your case, the overload that takes a stream as an argument:

System.Drawing.Image.Save(System.IO.Stream,
   System.Drawing.Imaging.ImageCodecInfo,
   System.Drawing.Imaging.EncoderParameters)
(http://msdn2.microsoft.com/en-gb/library/ms142148(VS.80).aspx)

The System.Drawing.Imaging.ImageCodecInfo class
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.imagecodecinfo(V
S.80).aspx
)
is easy to create, using the static ImageCodecInfo.GetEncoders method, and
using a MIME string. Here's a method that creates one:

public static ImageCodecInfo GetEncoderInfo(string mimeType)
{
   int intCt;

   ImageCodecInfo[] aryEncoders = ImageCodecInfo.GetImageEncoders();
   for (intCt = 0; intCt < aryEncoders.Length; intCt++)
   {
       if (aryEncoders[intCt].MimeType == mimeType)
           return aryEncoders[intCt];
   }
   throw new Exception("MimeType '" + mimeType + "' not found");
}

In the case of a BitMap, you would use "image/bmp" as the MIME string:

ImageCodeInfo codecInfo = GetEncoderInfo("image/bmp");

As to the EncoderParameters, the Encoder class
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.encoderparameter
(VS.80).aspx
)
provides a parameter for the image encoder used by the Save method, and you
can create and use any number of them. The constructor for EncoderParameter
usually takes an Encoder
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.encoder.encoder(
VS.80).aspx
)
and a value. There are a number of constructor overloads
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.encoder.encoder(
VS.80).aspx
)
to handle the different types of Encoders
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.encoder_members(
VS.80).aspx
)
available. The most commonly-used values for Encoders can be found in the
System.Drawing.Imaging.EncoderValue enumeration
(http://msdn2.microsoft.com/en-gb/library/system.drawing.imaging.encodervalue(VS.
80).aspx
).

Signature

HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.

> Thanks Kevin, I'd already read most of Bob's stuff and I didn't find
> anything directly on point.
[quoted text clipped - 19 lines]
>>> arrayImage = ms.GetBuffer();
>>> ms.Close();
Earl - 03 Dec 2006 01:10 GMT
Thanks for the links. I'm sure they will come in handy.

> Hi Earl,
>
[quoted text clipped - 68 lines]
>>>> arrayImage = ms.GetBuffer();
>>>> ms.Close();
Marc Gravell - 02 Dec 2006 14:59 GMT
At what point (in that sample) does the error occur? I don't know if it
is related, but you might wish to use either ms.ToArray(), or read
ms.Length before closing the stream, as ms.GetBuffer() will return
random data (probably all 0s) from the end of the oversized buffer that
MemoryStream allocates.

If this arrayImage is what you are saving to the database and then
reloading, then this could explain the errors, as the "image" contains
garbage at the end.

Marc
Earl - 02 Dec 2006 23:13 GMT
Have to use an ImageFormat instead of RawFormat. Apparently the file holds
the encoder params but those must be provided by the ImageFormat if saving
from the database or newly created bitmap.

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

>I have an image issue that I do not understand. When I try to save a bitmap
>created from a picturebox image, I can save without exception so long as
[quoted text clipped - 11 lines]
> arrayImage = ms.GetBuffer();
> ms.Close();
Kevin Spencer - 03 Dec 2006 00:02 GMT
I'm glad that worked for you, Earl. Keep the other information I gave you
(in my last reply) about ImageCodecInfo and EncoderParameters for future
reference!

Signature

HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.

> Have to use an ImageFormat instead of RawFormat. Apparently the file holds
> the encoder params but those must be provided by the ImageFormat if saving
[quoted text clipped - 18 lines]
>> arrayImage = ms.GetBuffer();
>> ms.Close();

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.