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

asp.net "the object is currently in use elsewhere"

Dear All,

(First of all this is not a c# piece of code but it does not really
matter).

I would really appreciate if someone could help me.
I am developing an ASP.NET web site and I have to deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
The following piece of code attempts to reduce the image size in order
to generate the thumbnail.
Everything seems to work fine with Internet Explorer 6 but, using
Firefox, I receive the following exception:
"The object is currently in use elsewhere" when trying to save the
bitmap in the MemoryStream.

I don't understand.

Many thanks in advance, for any help.

Didier

'************************************************* ********************
'
' pf_ModifyImage Method
'
' Modifies the width or height of an image.
'

'************************************************* ********************
Private Function pf_ModifyImage(ByVal original() As Byte, ByVal
width As Integer, ByVal height As Integer, ByVal contentType As String)
As Byte()

' original: byte array of the image
' width: new image width
' height: new image height
' contentType: type of image (this should be either pjpeg or
gif)

' convert byte array to image
Dim stream As New MemoryStream(original)

' convert size to new dimensions
Dim bmp As Bitmap = CType(Image.FromStream(stream), Bitmap)

' Calculate missing width or height
If width = -1 Then
width = Fix(System.Convert.ToDouble(height) /
System.Convert.ToDouble(bmp.Height) *
System.Convert.ToDouble(bmp.Width))
End If
If height = -1 Then
height = Fix(System.Convert.ToDouble(width) /
System.Convert.ToDouble(bmp.Width) *
System.Convert.ToDouble(bmp.Height))
End If

Dim thumbBmp As New Bitmap(bmp, width, height)
thumbBmp.Palette = pf_GetTransparentColorPalette(bmp)

' convert to stream in preparation to convert to byte array
Dim stream2 As MemoryStream = New MemoryStream

' save the stream
' first we need to determine if image is JPG or GIF
' to determine the proper encoder type

If contentType.ToLower().EndsWith("pjpeg") Then
thumbBmp.Save(stream2, ImageFormat.Jpeg)
Else
thumbBmp.Save(stream2, ImageFormat.Gif)
'<<<<<<<<<<<<<<< Exception occurs here.
End If
' cleanup
thumbBmp.Dispose()
bmp.Dispose()

Return stream2.GetBuffer()
End Function 'pf_ModifyImage

Private Function pf_GetTransparentColorPalette(ByVal original As
Bitmap) As ColorPalette
Dim testColor As Color
Dim newColor As Color
Dim pal As ColorPalette = original.Palette

Dim i As Integer
For i = 0 To (pal.Entries.Length - 1) - 1
testColor = pal.Entries(i)

If testColor.A = 0 Then
newColor = Color.FromArgb(0, testColor)
pal.Entries(i) = newColor
End If
Next i

Return pal
End Function 'pf_GetTransparentColorPalette

Nov 7 '06 #1
3 2191
booledi,
This is not a C# language newsgroup question. Post it over at the ASP.NET
group:

http://msdn.microsoft.com/newsgroups...k.aspnet&fltr=
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"boeledi" wrote:
Dear All,

(First of all this is not a c# piece of code but it does not really
matter).

I would really appreciate if someone could help me.
I am developing an ASP.NET web site and I have to deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
The following piece of code attempts to reduce the image size in order
to generate the thumbnail.
Everything seems to work fine with Internet Explorer 6 but, using
Firefox, I receive the following exception:
"The object is currently in use elsewhere" when trying to save the
bitmap in the MemoryStream.

I don't understand.

Many thanks in advance, for any help.

Didier

'************************************************* ********************
'
' pf_ModifyImage Method
'
' Modifies the width or height of an image.
'

'************************************************* ********************
Private Function pf_ModifyImage(ByVal original() As Byte, ByVal
width As Integer, ByVal height As Integer, ByVal contentType As String)
As Byte()

' original: byte array of the image
' width: new image width
' height: new image height
' contentType: type of image (this should be either pjpeg or
gif)

' convert byte array to image
Dim stream As New MemoryStream(original)

' convert size to new dimensions
Dim bmp As Bitmap = CType(Image.FromStream(stream), Bitmap)

' Calculate missing width or height
If width = -1 Then
width = Fix(System.Convert.ToDouble(height) /
System.Convert.ToDouble(bmp.Height) *
System.Convert.ToDouble(bmp.Width))
End If
If height = -1 Then
height = Fix(System.Convert.ToDouble(width) /
System.Convert.ToDouble(bmp.Width) *
System.Convert.ToDouble(bmp.Height))
End If

Dim thumbBmp As New Bitmap(bmp, width, height)
thumbBmp.Palette = pf_GetTransparentColorPalette(bmp)

' convert to stream in preparation to convert to byte array
Dim stream2 As MemoryStream = New MemoryStream

' save the stream
' first we need to determine if image is JPG or GIF
' to determine the proper encoder type

If contentType.ToLower().EndsWith("pjpeg") Then
thumbBmp.Save(stream2, ImageFormat.Jpeg)
Else
thumbBmp.Save(stream2, ImageFormat.Gif)
'<<<<<<<<<<<<<<< Exception occurs here.
End If
' cleanup
thumbBmp.Dispose()
bmp.Dispose()

Return stream2.GetBuffer()
End Function 'pf_ModifyImage

Private Function pf_GetTransparentColorPalette(ByVal original As
Bitmap) As ColorPalette
Dim testColor As Color
Dim newColor As Color
Dim pal As ColorPalette = original.Palette

Dim i As Integer
For i = 0 To (pal.Entries.Length - 1) - 1
testColor = pal.Entries(i)

If testColor.A = 0 Then
newColor = Color.FromArgb(0, testColor)
pal.Entries(i) = newColor
End If
Next i

Return pal
End Function 'pf_GetTransparentColorPalette

Nov 8 '06 #2
Boeledi,

A little bit much work if you use the C# language (or another dotNet program
language), there is for that a thumbnail method in the drawing part

http://msdn2.microsoft.com/en-us/lib...nailimage.aspx

however if your question is about the dotNet language VB.Net than you can
better ask this in future in the newsgroup
microsoft.public.dotnet.languages.vb

I hope this helps,

Cor

"boeledi" <di************@steams.beschreef in bericht
news:11**********************@h54g2000cwb.googlegr oups.com...
Dear All,

(First of all this is not a c# piece of code but it does not really
matter).

I would really appreciate if someone could help me.
I am developing an ASP.NET web site and I have to deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
The following piece of code attempts to reduce the image size in order
to generate the thumbnail.
Everything seems to work fine with Internet Explorer 6 but, using
Firefox, I receive the following exception:
"The object is currently in use elsewhere" when trying to save the
bitmap in the MemoryStream.

I don't understand.

Many thanks in advance, for any help.

Didier

'************************************************* ********************
'
' pf_ModifyImage Method
'
' Modifies the width or height of an image.
'

'************************************************* ********************
Private Function pf_ModifyImage(ByVal original() As Byte, ByVal
width As Integer, ByVal height As Integer, ByVal contentType As String)
As Byte()

' original: byte array of the image
' width: new image width
' height: new image height
' contentType: type of image (this should be either pjpeg or
gif)

' convert byte array to image
Dim stream As New MemoryStream(original)

' convert size to new dimensions
Dim bmp As Bitmap = CType(Image.FromStream(stream), Bitmap)

' Calculate missing width or height
If width = -1 Then
width = Fix(System.Convert.ToDouble(height) /
System.Convert.ToDouble(bmp.Height) *
System.Convert.ToDouble(bmp.Width))
End If
If height = -1 Then
height = Fix(System.Convert.ToDouble(width) /
System.Convert.ToDouble(bmp.Width) *
System.Convert.ToDouble(bmp.Height))
End If

Dim thumbBmp As New Bitmap(bmp, width, height)
thumbBmp.Palette = pf_GetTransparentColorPalette(bmp)

' convert to stream in preparation to convert to byte array
Dim stream2 As MemoryStream = New MemoryStream

' save the stream
' first we need to determine if image is JPG or GIF
' to determine the proper encoder type

If contentType.ToLower().EndsWith("pjpeg") Then
thumbBmp.Save(stream2, ImageFormat.Jpeg)
Else
thumbBmp.Save(stream2, ImageFormat.Gif)
'<<<<<<<<<<<<<<< Exception occurs here.
End If
' cleanup
thumbBmp.Dispose()
bmp.Dispose()

Return stream2.GetBuffer()
End Function 'pf_ModifyImage

Private Function pf_GetTransparentColorPalette(ByVal original As
Bitmap) As ColorPalette
Dim testColor As Color
Dim newColor As Color
Dim pal As ColorPalette = original.Palette

Dim i As Integer
For i = 0 To (pal.Entries.Length - 1) - 1
testColor = pal.Entries(i)

If testColor.A = 0 Then
newColor = Color.FromArgb(0, testColor)
pal.Entries(i) = newColor
End If
Next i

Return pal
End Function 'pf_GetTransparentColorPalette

Nov 8 '06 #3
To change this,

I saw now that you asked this question as well in the language.vb newsgroup
as almost the same time, what is the reason you ask this question here?

Cor

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:eS**************@TK2MSFTNGP03.phx.gbl...
Boeledi,

A little bit much work if you use the C# language (or another dotNet
program language), there is for that a thumbnail method in the drawing
part

http://msdn2.microsoft.com/en-us/lib...nailimage.aspx

however if your question is about the dotNet language VB.Net than you can
better ask this in future in the newsgroup
microsoft.public.dotnet.languages.vb

I hope this helps,

Cor

"boeledi" <di************@steams.beschreef in bericht
news:11**********************@h54g2000cwb.googlegr oups.com...
>Dear All,

(First of all this is not a c# piece of code but it does not really
matter).

I would really appreciate if someone could help me.
I am developing an ASP.NET web site and I have to deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
The following piece of code attempts to reduce the image size in order
to generate the thumbnail.
Everything seems to work fine with Internet Explorer 6 but, using
Firefox, I receive the following exception:
"The object is currently in use elsewhere" when trying to save the
bitmap in the MemoryStream.

I don't understand.

Many thanks in advance, for any help.

Didier

'************************************************ *********************
'
' pf_ModifyImage Method
'
' Modifies the width or height of an image.
'

'************************************************ *********************
Private Function pf_ModifyImage(ByVal original() As Byte, ByVal
width As Integer, ByVal height As Integer, ByVal contentType As String)
As Byte()

' original: byte array of the image
' width: new image width
' height: new image height
' contentType: type of image (this should be either pjpeg or
gif)

' convert byte array to image
Dim stream As New MemoryStream(original)

' convert size to new dimensions
Dim bmp As Bitmap = CType(Image.FromStream(stream), Bitmap)

' Calculate missing width or height
If width = -1 Then
width = Fix(System.Convert.ToDouble(height) /
System.Convert.ToDouble(bmp.Height) *
System.Convert.ToDouble(bmp.Width))
End If
If height = -1 Then
height = Fix(System.Convert.ToDouble(width) /
System.Convert.ToDouble(bmp.Width) *
System.Convert.ToDouble(bmp.Height))
End If

Dim thumbBmp As New Bitmap(bmp, width, height)
thumbBmp.Palette = pf_GetTransparentColorPalette(bmp)

' convert to stream in preparation to convert to byte array
Dim stream2 As MemoryStream = New MemoryStream

' save the stream
' first we need to determine if image is JPG or GIF
' to determine the proper encoder type

If contentType.ToLower().EndsWith("pjpeg") Then
thumbBmp.Save(stream2, ImageFormat.Jpeg)
Else
thumbBmp.Save(stream2, ImageFormat.Gif)
'<<<<<<<<<<<<<<< Exception occurs here.
End If
' cleanup
thumbBmp.Dispose()
bmp.Dispose()

Return stream2.GetBuffer()
End Function 'pf_ModifyImage

Private Function pf_GetTransparentColorPalette(ByVal original As
Bitmap) As ColorPalette
Dim testColor As Color
Dim newColor As Color
Dim pal As ColorPalette = original.Palette

Dim i As Integer
For i = 0 To (pal.Entries.Length - 1) - 1
testColor = pal.Entries(i)

If testColor.A = 0 Then
newColor = Color.FromArgb(0, testColor)
pal.Entries(i) = newColor
End If
Next i

Return pal
End Function 'pf_GetTransparentColorPalette


Nov 8 '06 #4

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

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
2
by: FrzzMan | last post by:
The first time I called this function, everything went well, but the second time I called it. An Exception thrown, do you know why? An unhandled exception of type 'System.IO.IOException' occurred...
4
by: Mau Kae Horng | last post by:
Hello, I have a C# Windows Forms application for machine. Due to some unknown reasons, the application face problems with unexpected exceptions happening, resulting in two red lines forming a...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
10
by: Ole | last post by:
Hi, Using VS2005 and a windows CE 5.0 device running CF2. Suddenly I can't debug my C# program from VS2005 - when setting a breakpoint I only see a ring instead of the normal red dot and when...
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
1
by: boeledi | last post by:
Dear All, I would really appreciate if someone could help me. I am developing an ASP.NET web site and I have to deal with images upload. When the user is selecting an image, I save it as such...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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...
0
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...

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.