473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Still playing with bitmap images

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();
Dec 2 '06 #1
7 2033
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

Dec 2 '06 #2
You should find the following resource very helpful:

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

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
>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();

Dec 2 '06 #3
Thanks Kevin, I'd already read most of Bob's stuff and I didn't find
anything directly on point.

"Kevin Spencer" <sp**@uce.govwrote in message
news:O7**************@TK2MSFTNGP04.phx.gbl...
You should find the following resource very helpful:

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

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
>>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();


Dec 2 '06 #4
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);

"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
>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();

Dec 2 '06 #5
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/lib...48(VS.80).aspx)

The System.Drawing.Imaging.ImageCodecInfo class
(http://msdn2.microsoft.com/en-gb/lib...fo(VS.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/lib...er(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/lib...er(VS.80).aspx)
and a value. There are a number of constructor overloads
(http://msdn2.microsoft.com/en-gb/lib...er(VS.80).aspx)
to handle the different types of Encoders
(http://msdn2.microsoft.com/en-gb/lib...rs(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/lib...e(VS.80).aspx).

--
HTH,

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

Parabola is a mate of plane.

"Earl" <br******@newsgroups.nospamwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
Thanks Kevin, I'd already read most of Bob's stuff and I didn't find
anything directly on point.

"Kevin Spencer" <sp**@uce.govwrote in message
news:O7**************@TK2MSFTNGP04.phx.gbl...
>You should find the following resource very helpful:

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

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
>>>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();



Dec 2 '06 #6
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!

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsgroups.nospamwrote in message
news:uy**************@TK2MSFTNGP04.phx.gbl...
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);

"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
>>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();


Dec 2 '06 #7
Thanks for the links. I'm sure they will come in handy.

"Kevin Spencer" <sp**@uce.govwrote in message
news:e3**************@TK2MSFTNGP03.phx.gbl...
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/lib...48(VS.80).aspx)

The System.Drawing.Imaging.ImageCodecInfo class
(http://msdn2.microsoft.com/en-gb/lib...fo(VS.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/lib...er(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/lib...er(VS.80).aspx)
and a value. There are a number of constructor overloads
(http://msdn2.microsoft.com/en-gb/lib...er(VS.80).aspx)
to handle the different types of Encoders
(http://msdn2.microsoft.com/en-gb/lib...rs(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/lib...e(VS.80).aspx).

--
HTH,

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

Parabola is a mate of plane.

"Earl" <br******@newsgroups.nospamwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
>Thanks Kevin, I'd already read most of Bob's stuff and I didn't find
anything directly on point.

"Kevin Spencer" <sp**@uce.govwrote in message
news:O7**************@TK2MSFTNGP04.phx.gbl...
>>You should find the following resource very helpful:

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

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsgroups.nospamwrote in message
news:uq**************@TK2MSFTNGP06.phx.gbl...
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();



Dec 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Erwin | last post by:
At the moment I'm using a report which contains an indicator to show if a Service group of the company isn't working well or is working perfectly. This indicator is a "*" which looks like a traffic...
2
by: Mad Scientist Jr | last post by:
I have a bitmap (32 pixels high, 8192 pixels wide) that contains 255 images, each 32 pixels wide, that I would like to chop up into individual 32x32 bitmap files. Rather than spending hours in...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
2
by: GT | last post by:
Could someone please explain how to add images to a ListView other than in the first column?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.