473,320 Members | 2,107 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,320 software developers and data experts.

Problems changing image resolution in VB.NET

I'm reading a series of images from a MS SQL table and saving them to
directory. These are staff ID pictures from our security card app.
Once I've extracted the ID photo from the security app to disk, I need
to reference the disk file in our HR app. As part of the process, I'm
resizing the image and changing its resolution from 96 to 72 dpi. If
they are not at a 72 dpi resolution, the HR app freezes. The resizing
works without a hitch, but I cannot figure out how to reduce the
resolution. All of the output files continue to be 96 dpi. Any help
would be greatly appreciated.

Here's the code

<snip...>
Dim bytImage() As Byte
bytImage = .ItemArray(4)
bytImage = ChangeImageResolution(bytImage, 72)
bytImage = ResizeImage(bytImage, 150)
Dim fs As New FileStream(strFileName, FileMode.OpenOrCreate,
FileAccess.Write)
fs.Write(bytImage, 0, UBound(bytImage))
fs.Close()
fs = Nothing
<snip...>
Private Function ChangeImageResolution(ByVal bytInput As Byte(), ByVal
intOutputResolution As Int16) As Byte()
Dim strmInput As New System.IO.MemoryStream(bytInput)
Dim strmOutput As New System.IO.MemoryStream
Dim imgInput As System.Drawing.Image
Dim imgOutput As System.Drawing.Image
Dim bmapTemp As Bitmap

' Create a temporary bitmap and set to output resolution
imgInput = System.Drawing.Image.FromStream(strmInput)
bmapTemp = imgInput
bmapTemp.SetResolution(intOutputResolution, intOutputResolution)
imgOutput = New Bitmap(bmapTemp)
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)

' Return the output image in a byte array
ChangeImageResolution = strmOutput.ToArray
End Function
Private Function ResizeImage(ByVal bytInput As Byte(), ByVal
intFinalMaxDim As Int16) As Byte()
Dim strmInput As New System.IO.MemoryStream(bytInput)
Dim strmOutput As New System.IO.MemoryStream
Dim imgInput As System.Drawing.Image
Dim imgOutput As System.Drawing.Image
Dim intMaxDim As Int16 = 0
Dim dblResizePercentage As Double = 0
Dim sizResize As New Size

' Determine the resizing percentage based on current image
dimensions
imgInput = System.Drawing.Image.FromStream(strmInput)
If imgInput.Height >= imgInput.Width Then
intMaxDim = imgInput.Height
Else
intMaxDim = imgInput.Width
End If
dblResizePercentage = (intFinalMaxDim / intMaxDim)
With sizResize
.Width = CInt(imgInput.Width * dblResizePercentage)
.Height = CInt(imgInput.Height * dblResizePercentage)
End With

' Create a new resized version of the image
imgOutput = New Bitmap(imgInput, sizResize.Width,
sizResize.Height)
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)

' Return the output image in a byte array
ResizeImage = strmOutput.ToArray
End Function

Oct 1 '07 #1
9 9548
<ko*****@hotmail.comschrieb
I'm reading a series of images from a MS SQL table and saving them
to directory. These are staff ID pictures from our security card
app. Once I've extracted the ID photo from the security app to disk,
I need to reference the disk file in our HR app. As part of the
process, I'm resizing the image and changing its resolution from 96
to 72 dpi. If they are not at a 72 dpi resolution, the HR app
freezes. The resizing works without a hitch, but I cannot figure out
how to reduce the resolution. All of the output files continue to be
96 dpi. Any help would be greatly appreciated.
[...]
imgInput = System.Drawing.Image.FromStream(strmInput)
bmapTemp = imgInput
I guess you know that the line above only copies the reference, not the
image. (?) (but that's not the problem, see below)
bmapTemp.SetResolution(intOutputResolution, intOutputResolution)
imgOutput = New Bitmap(bmapTemp)
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)

You should set the resolution of the new bitmap (imgOutput):

imgOutput = New Bitmap(bmapTemp)
imgOutput.SetResolution(intOutputResolution, intOutputResolution)
Armin

Oct 1 '07 #2
On Oct 1, 2:51 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
>
You should set the resolution of the new bitmap (imgOutput):

imgOutput = New Bitmap(bmapTemp)
imgOutput.SetResolution(intOutputResolution, intOutputResolution)

Armin
Armin,

Thanks for your reply. I was trying to convert imgInput from a
System.Drawing.Image (imgInput) to a Bitmap (bmapTemp), then use the
SetResolution method, and finally convert bmapTemp back to a
System.Drawing.Image (imgOutput).

I've tried your suggestion of using SetResolution on imgOutput, but it
doesn't work because imgOutput is a System.Drawing.Image object.

Have I misunderstood you?

Oct 1 '07 #3
<ko*****@hotmail.comschrieb
On Oct 1, 2:51 pm, "Armin Zingler" <az.nos...@freenet.dewrote:

You should set the resolution of the new bitmap (imgOutput):

imgOutput = New Bitmap(bmapTemp)
imgOutput.SetResolution(intOutputResolution,
intOutputResolution)

Armin

Armin,

Thanks for your reply. I was trying to convert imgInput from a
System.Drawing.Image (imgInput) to a Bitmap (bmapTemp), then use the
SetResolution method, and finally convert bmapTemp back to a
System.Drawing.Image (imgOutput).

I've tried your suggestion of using SetResolution on imgOutput, but
it doesn't work because imgOutput is a System.Drawing.Image object.

Have I misunderstood you?
I didn't see that imtOutput is declared as Image. You can declare it As
Bitmap because it's always a Bitmap in the code you posted.
Armin

Oct 2 '07 #4
On Oct 1, 5:53 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<komb...@hotmail.comschrieb


On Oct 1, 2:51 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
You should set theresolutionof the new bitmap (imgOutput):
imgOutput = New Bitmap(bmapTemp)
imgOutput.SetResolution(intOutputResolution,
intOutputResolution)
Armin
Armin,
Thanks for your reply. I was trying to convert imgInput from a
System.Drawing.Image(imgInput) to a Bitmap (bmapTemp), then use the
SetResolution method, and finally convert bmapTemp back to a
System.Drawing.Image(imgOutput).
I've tried your suggestion of using SetResolution on imgOutput, but
it doesn't work because imgOutput is a System.Drawing.Imageobject.
Have I misunderstood you?

I didn't see that imtOutput is declared asImage. You can declare it As
Bitmap because it's always a Bitmap in the code you posted.

Armin- Hide quoted text -

- Show quoted text -
Armin,

Thanks for your message. I see your point about the imgOutput object.
When I dimension imgOutput as a bitmap and examine the result in the
Watch window, it is a bitmap object with a 72 dpi resolution.

Apparently I'm not understanding the System.Drawing.Image.Save method.
The result of these lines is a byte array containing a 96 dpi jpeg.

imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray

Do you have any suggestions of how to translate the 72 dpi bitmap to a
72 dpi jpeg?

Oct 2 '07 #5
<ko*****@hotmail.comschrieb
Armin,

Thanks for your message. I see your point about the imgOutput
object. When I dimension imgOutput as a bitmap and examine the
result in the Watch window, it is a bitmap object with a 72 dpi
resolution.

Apparently I'm not understanding the System.Drawing.Image.Save
method. The result of these lines is a byte array containing a 96
dpi jpeg.

imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray

Do you have any suggestions of how to translate the 72 dpi bitmap to
a 72 dpi jpeg?
Aaahh, another thing I didn't see. You're clandestinly converting to a JPG
when saving. ;-) I did some research but didn't find how to do it. I only
know how to specify the compression quality and other parameters but not the
resolution. Maybe m.p.dotnet.framework.drawing has an answer.
Armin

Oct 2 '07 #6
On Oct 2, 12:40 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<komb...@hotmail.comschrieb


Armin,
Thanks for your message. I see your point about the imgOutput
object. When I dimension imgOutput as a bitmap and examine the
result in the Watch window, it is a bitmap object with a 72 dpi
resolution.
Apparently I'm not understanding the System.Drawing.Image.Save
method. The result of these lines is a byte array containing a 96
dpi jpeg.
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray
Do you have any suggestions of how to translate the 72 dpi bitmap to
a 72 dpi jpeg?

Aaahh, another thing I didn't see. You're clandestinly converting to a JPG
when saving. ;-) I did some research but didn't find how to do it. I only
know how to specify the compression quality and other parameters but not the
resolution. Maybe m.p.dotnet.framework.drawing has an answer.

Armin- Hide quoted text -

- Show quoted text -
Thanks, Armin. I really appreciate your help with this. I'll try the
m.p.dotnet.framework.drawing group as you suggest.

Oct 2 '07 #7
Try looking at "codecs".
--
Dennis in Houston
"ko*****@hotmail.com" wrote:
On Oct 2, 12:40 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<komb...@hotmail.comschrieb


Armin,
Thanks for your message. I see your point about the imgOutput
object. When I dimension imgOutput as a bitmap and examine the
result in the Watch window, it is a bitmap object with a 72 dpi
resolution.
Apparently I'm not understanding the System.Drawing.Image.Save
method. The result of these lines is a byte array containing a 96
dpi jpeg.
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray
Do you have any suggestions of how to translate the 72 dpi bitmap to
a 72 dpi jpeg?
Aaahh, another thing I didn't see. You're clandestinly converting to a JPG
when saving. ;-) I did some research but didn't find how to do it. I only
know how to specify the compression quality and other parameters but not the
resolution. Maybe m.p.dotnet.framework.drawing has an answer.

Armin- Hide quoted text -

- Show quoted text -

Thanks, Armin. I really appreciate your help with this. I'll try the
m.p.dotnet.framework.drawing group as you suggest.

Oct 3 '07 #8
On Oct 2, 5:11 pm, Dennis <Den...@discussions.microsoft.comwrote:
Try looking at "codecs".
--
Dennis in Houston

"komb...@hotmail.com" wrote:
On Oct 2, 12:40 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<komb...@hotmail.comschrieb
Armin,
Thanks for your message. I see your point about the imgOutput
object. When I dimension imgOutput as a bitmap and examine the
result in the Watch window, it is a bitmap object with a 72 dpi
resolution.
Apparently I'm not understanding the System.Drawing.Image.Save
method. The result of these lines is a byte array containing a 96
dpi jpeg.
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the outputimagein a byte array
ResizeImage = strmOutput.ToArray
Do you have any suggestions of how to translate the 72 dpi bitmap to
a 72 dpi jpeg?
Aaahh, another thing I didn't see. You're clandestinly converting to a JPG
when saving. ;-) I did some research but didn't find how to do it. I only
know how to specify the compression quality and other parameters but not the
>resolution. Maybe m.p.dotnet.framework.drawing has an answer.
Armin- Hide quoted text -
- Show quoted text -
Thanks, Armin. I really appreciate your help with this. I'll try the
m.p.dotnet.framework.drawing group as you suggest.- Hide quoted text -

- Show quoted text -
Dennis,

What do you mean by codecs? How does it relate to images & bitmaps?

Oct 3 '07 #9
You can convert to a bitmap then back to a JPEG using Codecs. With the
bitmap you might try:

Dim bm As New Bitmap
'Convert your image using Codecs into bm
bm.SetResolution(72, 72)
bm.Save("C:\test1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
or
bm.SetResolution(100, 100)
bm.Save("C:\test2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Using Codecs allows control over image quality, etc. also. An example of
Codec usage:

Public Class CvImage

Shared codecs() As ImageCodecInfo

Public Sub New()
'Get the list of available encoders
Static Init As Boolean = True
If Init Then codecs = ImageCodecInfo.GetImageEncoders() : Init = False
End Sub
Public Function BitMapToJpeg(ByVal img As Image, Optional ByVal quality
As Integer = 100) As Image
Dim ici As ImageCodecInfo
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
ici = codec
End If
Next
Dim ep As EncoderParameters = New EncoderParameters(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParameter(Encoder.Quality, Quality)
'Save to a Memory Stream
Dim s As New System.IO.MemoryStream
' Dim byt() As Byte
img.Save(s, ici, ep)
Return img.FromStream(s)
End Function
Public Function ImageToJpegToBytes(ByVal img As Image, Optional ByVal
Quality As Integer = 100) As Byte()
'find the encoder with the image/jpeg mime-type
Dim ici As ImageCodecInfo
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
ici = codec
End If
Next
Dim ep As EncoderParameters = New EncoderParameters(1)
'Set Quality Parameter
ep.Param(0) = New EncoderParameter(Encoder.Quality, Quality)
'Save to a Memory Stream
Dim s As New System.IO.MemoryStream
Dim byt() As Byte
img.Save(s, ici, ep)
'Convert Memory Stream to byte array
Return s.ToArray
End Function
End Class
If you can't work something with the above, you might try something along
the following:
- Create graphics object with the DPIX and DPIY you want of the pixel size
of the image
- Use BitBlt to copy your bitmap into the graphics object

Just some ideas that might work....
--
Dennis in Houston
"ko*****@hotmail.com" wrote:
On Oct 2, 5:11 pm, Dennis <Den...@discussions.microsoft.comwrote:
Try looking at "codecs".
--
Dennis in Houston

"komb...@hotmail.com" wrote:
On Oct 2, 12:40 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<komb...@hotmail.comschrieb
Armin,
Thanks for your message. I see your point about the imgOutput
object. When I dimension imgOutput as a bitmap and examine the
result in the Watch window, it is a bitmap object with a 72 dpi
>resolution.
Apparently I'm not understanding the System.Drawing.Image.Save
method. The result of these lines is a byte array containing a 96
dpi jpeg.
imgOutput.Save(strmOutput,
System.Drawing.Imaging.ImageFormat.Jpeg)
' Return the outputimagein a byte array
ResizeImage = strmOutput.ToArray
Do you have any suggestions of how to translate the 72 dpi bitmap to
a 72 dpi jpeg?
Aaahh, another thing I didn't see. You're clandestinly converting to a JPG
when saving. ;-) I did some research but didn't find how to do it. I only
know how to specify the compression quality and other parameters but not the
resolution. Maybe m.p.dotnet.framework.drawing has an answer.
Armin- Hide quoted text -
- Show quoted text -
Thanks, Armin. I really appreciate your help with this. I'll try the
m.p.dotnet.framework.drawing group as you suggest.- Hide quoted text -
- Show quoted text -

Dennis,

What do you mean by codecs? How does it relate to images & bitmaps?

Oct 5 '07 #10

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

Similar topics

3
by: AlexGreif.2609394 | last post by:
Hi, I looked at the PIL Image class but cannot see a posibility to retreive the image resolution dots per inch (or pixels per inch) Could somebody please tell me how to get these metadata infos...
3
by: Bernd Giegerich | last post by:
Hi, we had a strange problem with DB2 8.2 Enterprise Edition on Windows 2003 (Standard) Server. We installed DB2 8.2 (8.1.7) directly on a naked W2K3 system (no migration, no update -> no...
5
by: jlombardo | last post by:
I've read previous posts and know it is "bad form" to change a user's screen resolution to fit your program's needs. My situation is very different. I have a user with a laptop resolution of...
0
by: Panic Student | last post by:
hi all, i'm having trouble with changing image when mouseover on a particular label. Below is what i attempted to but it didn't seem to work at all. I guess my concept is wrong ): Please...
0
by: Airw0lf | last post by:
I am currently finding the resolution of an image as follows: System::Drawing::Image *img; img = System::Drawing::Image::FromFile(fn); I can then make use of img->Width, img->Height etc. The...
4
by: CG3000 | last post by:
I create a .PNG image ( in Macromedia Fireworks ) which has an gif in it in the top left corner and a lot of empty canvas space to the right. I use about 10 text boxes on a form to populate...
2
by: almurph | last post by:
Hi, Hope you can help me wit this one. I have to open either a ".jpg", ".bmp" or a "gif" and alter its dimensions and save the output as a .gif. Here is my attempt - I would greatly...
7
by: Trastabuga | last post by:
I have a simple task: I need to change image src on the fly and then get its new width and height; var url = "/some/request"; var img = document.getElementById('my_img'); var old_width =...
4
JustRun
by: JustRun | last post by:
Hi all, I have a file button that enable user to choose the image, then save it to the datagridview, The problem is the resolution of the image After saving go down but before saving it good, ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.