473,387 Members | 1,464 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.

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

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 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
1 1080
Boeledi,

Will you please not multipost, I have answered your question in another
newsgroup.

Cor

"boeledi" <di************@steams.beschreef in bericht
news:11********************@h48g2000cwc.googlegrou ps.com...
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 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

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...
1
by: J. Muenchbourg | last post by:
I have a label object that gets a dynamic value thrown into it on a button submit event: lblMessage.InnerText = txtName.Value but i get "Object reference not set to an instance of an object"...
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...
3
by: boeledi | last post by:
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...
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
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: 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
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...
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...

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.