473,666 Members | 2,039 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 2064
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
2297
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
6758
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
3313
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
2300
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
4261
by: GT | last post by:
Could someone please explain how to add images to a ListView other than in the first column?
0
8444
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
8781
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
8551
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,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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
2771
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

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.