473,839 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.RawFor mat);
arrayImage = ms.GetBuffer();
ms.Close();
Dec 2 '06 #1
7 2073
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******@newsg roups.nospamwro te in message
news:uq******** ******@TK2MSFTN GP06.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.RawFor mat);
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.govwr ote in message
news:O7******** ******@TK2MSFTN GP04.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******@newsg roups.nospamwro te in message
news:uq******** ******@TK2MSFTN GP06.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.RawFor mat);
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.ImageFo rmat.Bmp);

"Earl" <br******@newsg roups.nospamwro te in message
news:uq******** ******@TK2MSFTN GP06.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.RawFor mat);
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 EncoderParamete rs
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(Syst em.IO.Stream,
System.Drawing. Imaging.ImageCo decInfo,
System.Drawing. Imaging.Encoder Parameters)
(http://msdn2.microsoft.com/en-gb/lib...48(VS.80).aspx)

The System.Drawing. Imaging.ImageCo decInfo 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. GetImageEncoder s();
for (intCt = 0; intCt < aryEncoders.Len gth; intCt++)
{
if (aryEncoders[intCt].MimeType == mimeType)
return aryEncoders[intCt];
}
throw new Exception("Mime Type '" + 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 EncoderParamete rs, 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 EncoderParamete r
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.Encoder Value 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******@newsg roups.nospamwro te in message
news:Ob******** ******@TK2MSFTN GP03.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.govwr ote in message
news:O7******** ******@TK2MSFTN GP04.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******@newsg roups.nospamwro te in message
news:uq******* *******@TK2MSFT NGP06.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);
MemoryStrea m ms = new MemoryStream();
bmp.Save(ms , pb.Image.RawFor mat);
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 EncoderParamete rs for future
reference!

--
HTH,

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

Parabola is a mate of plane.
"Earl" <br******@newsg roups.nospamwro te in message
news:uy******** ******@TK2MSFTN GP04.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.ImageFo rmat.Bmp);

"Earl" <br******@newsg roups.nospamwro te in message
news:uq******** ******@TK2MSFTN GP06.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.RawFor mat);
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.govwr ote in message
news:e3******** ******@TK2MSFTN GP03.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 EncoderParamete rs
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(Syst em.IO.Stream,
System.Drawing. Imaging.ImageCo decInfo,
System.Drawing. Imaging.Encoder Parameters)
(http://msdn2.microsoft.com/en-gb/lib...48(VS.80).aspx)

The System.Drawing. Imaging.ImageCo decInfo 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. GetImageEncoder s();
for (intCt = 0; intCt < aryEncoders.Len gth; intCt++)
{
if (aryEncoders[intCt].MimeType == mimeType)
return aryEncoders[intCt];
}
throw new Exception("Mime Type '" + 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 EncoderParamete rs, 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
EncoderParamete r 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.Encoder Value 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******@newsg roups.nospamwro te in message
news:Ob******** ******@TK2MSFTN GP03.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.govwr ote in message
news:O7******* *******@TK2MSFT NGP04.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******@newsg roups.nospamwro te in message
news:uq****** ********@TK2MSF TNGP06.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
picturebo x. But if I load the image from the database into the
picturebo x 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);
MemoryStre am ms = new MemoryStream();
bmp.Save(m s, pb.Image.RawFor mat);
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
2303
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 light. Example: When a Service groep does hit the target, the indicator will lit up using the following Control Source: =IIf(>0,8*10*;"*") But now, I would like the use a real bitmap, trafficlight bitmaps, to
2
6791
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 Paint or Photoshop I would like to do this programmatically. My code below attempts to load in the original bitmap, crop it at the desired location, and save it to the correct file. I don't think I'm doing this right, I tried messing with different...
8
3337
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 froggraphic As Graphics = Graphics.FromImage(frogbitmap) froggraphic.RotateTransform(90) frogbitmap.Save(Server.MapPath("images/frog2.gif"), Imaging.ImageFormat.Gif)
7
2326
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, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
2
4270
by: GT | last post by:
Could someone please explain how to add images to a ListView other than in the first column?
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10648
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.