Converting Bitmap to Byte then Byte to Array:Arithmetic operation resulted in an overflow.

Greetings,

       I have a problem that I have been trying to solve for the past 4 hours or so. What I am trying to do is first resize a bitmap image. I want to save that image into a database. To do so i've tried to convert the bitmap jpeg image into a byte. Then convert the byte info into an Int64 to be used to stream the image. However, everytime I try this technique I get the following error: Arithmetic operation resulted in an overflow. From my understanding this have to do with the fact tha tthe byte buffer is to small for the file size. Correct? Anyhow, any help would be appreciated. Thanks in advance

1    If checktype(FileUpload1.PostedFile.ContentType) Then
2   
3                        Dim
ImagetoUpload As Bitmap = (Resizeimage(imagestream, 375, 500))
4   
5                        Using ms As New MemoryStream 'Stream Store is memory
6   
7                            'Saves the newly formatted image to the memory stream object named ms
8   
9                            ImagetoUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
10                           'ms.Position = 0
11                           Dim abyte(ms.Length - 1) As Byte ' Bitmap to byte
12                           ms.Seek(0, SeekOrigin.Begin)
13                           ms.Read(abyte, 0, ms.Length) 'Bitmap to byte conversion
14  
15  
16                           INTnewimage = BitConverter.ToInt64(abyte, 0)
17  
18  
19                       End Using
20  
21  
22  
23                       Dim bytedata() As Byte
24  
25                       'Redim bytedata to the sizd of intimagelenght. ex.12kb
26                       ReDim bytedata(INTnewimage)
27  
28  
29                       'Reads from 0 to the size of Intimageslenght into bytedata
30                       imagestream.Read(bytedata, 0, INTnewimage)

 
0
SuperRom
5/3/2009 9:52:28 PM
asp.net.drawinggdi 1803 articles. 0 followers. Follow

2 Replies
1557 Views

Similar Articles

[PageSpeed] 16
Get it on Google Play
Get it on Apple App Store

Hi,

//Resizing image

http://www.neodynamic.com/ND/FaqsTipsTricks.aspx?tabid=66&prodid=4&sid=71

http://www.4guysfromrolla.com/articles/012203-1.aspx

Once u have Sezied the Image Know u can Save that image inside DatabaseBy using stream property Sample which is given bleow

//Know inerting Image in database

http://aspalliance.com/articleViewer.aspx?aId=138&pId=

http://www.codeproject.com/KB/web-image/PicManager.aspx

Hoppe It helps


Thanks
Avinash Tiwari

Remember to click “Mark as Answer” on the post, if it helps you.
0
qwe123kids
5/4/2009 10:13:02 AM

Thanks for the Info qwe123kids. However, my programmed worked previously before I decided I wanted to resize the images that is uploaded. It seems this time around that the buffer size cannot hold such large info. If I remove the resizing function and revert back to my original program it works flawlessly. Uploads and saves to database. Anyhow, below is my resizing function..

 

 

1     Function Resizeimage(ByVal Streamimage As Stream, ByVal maxWidth As Int64, ByVal maxHeight As Int64) As Bitmap
2    
3            Dim ogimage As New Bitmap(Streamimage)
4            Dim newWidth As Int64 = ogimage.Width
5            Dim newHeight As Int64 = ogimage.Height
6    
7            Dim aspectratio As Double = Double.Parse(ogimage.Width) / Double.Parse(ogimage.Height)
8    
9            If aspectratio <= 1 And ogimage.Width > maxWidth Then
10               newWidth = maxWidth
11               newHeight = CInt(Math.Round(newWidth / aspectratio))
12           Else
13               If aspectratio > 1 And ogimage.Height > maxHeight Then
14                   newHeight = maxHeight
15                   newWidth = CInt(Math.Round(newHeight * aspectratio))
16   
17   
18   
19               End If
20   
21           End If
22           Dim newImage As New Bitmap(ogimage, newWidth, newHeight)
23           Dim Graphx As Graphics = Graphics.FromImage(newImage)
24           Graphx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
25           Graphx.DrawImage(ogimage, 0, 0, newImage.Width, newImage.Height)
26           ogimage.Dispose()
27   
28   
29   
30           Return newImage
31   
32       End Function
 
0
SuperRom
5/4/2009 1:20:25 PM
Reply:

Similar Artilces:

How to convert object to Byte[] (Byte array)
Hello how are all here? I need to convert an object into byte[] (byte array). The line of code is below: byte[] DocImage = new byte[] DocImage = returnData["DocImage"] (which returns an object) Now How can I convert object to byte[]? There does not exist any built-in conversion from object to byte[] like Convert.ToInt32, Convert.ToString etc Thanks byte[] DocImage = new byte[] DocImage = (byte[] )returnData["DocImage"] Regards,Anas Ghanem.Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like dup...

convert bitmap to 1-dimensional array of bytes
I am trying to upload a image to SQL server Dim gGraph As System.Drawing.Image = System.Drawing.Image.FromFile(myFile.FileName) Dim imgOutput As New Bitmap(gGraph , 100, 100) WriteToDB(imgOutput) Private Function WriteToDB(ByVal Buffer As Byte()) Dim objCOmmand As New SqlCommand("StoreP", "connection") objConnection.Open() objCOmmand.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)) objCOmmand.Parameters("@ActualPic").Value = Buffer objCOmmand.ExecuteNonQuery...

Weird result converting byte array to unicode and back
 I have the following piece of code which converts an array of bytes to a unicode string and back to a byte array. However for certain byte values they don't map correctly? 1 Dim testArr() As Byte = {108, 84, 177, 173, 60, 36, 200, 240, 63, 218, 156, 141, 119, 61, 100, 232, 38, 222, 26, 148, 133, 70, 226, 252, 209, 79, 202, 208, 62, 197, 223, 4}2 Dim encUni As New System.Text.UnicodeEncoding 3 dim vecStr as string = encUni.GetString(majorTestArra)4 Response.Write("vector string: " & vecStr & "<br />")5 Response.Write("vec...

how to convert array of ansichar to array to byte?
Hi, I have a very hard time to convert array of ansichar to array to byte. I tried {code} arr: array[0..32767] of AnsiChar; bArr: TByteArray; move(arr, bArr, numBytesToRead); {code} and it doesn't work. they are array of 8 bit and I wonder what is the correct way to do this. Is there a way to cast this as well? I have search all over the internet and couldn't find any information about this. Could anybody help me? thanks alot, edwin chang wrote: > Hi, I have a very hard time to convert array of ansichar to array to byte. > I tried > > {code} ...

Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'.
Hi Anybody can help me Hi, I get the exception message Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'. however i'm getting error here Dim obj As Byte() = CByte(cmd.ExecuteScalar()) Response.ContentType = "Application/msword" Response.BinaryWrite(CByte(cmd.ExecuteScalar())) I have run the code below Dim stream As New IO.MemoryStream() Dim strcon As String = "Data Source=(local) ;Initial Catalog=SaveImage; User ID=sa; Password=sa;"Dim con As New SqlConnection(strcon) con.Open()Dim cmd As New SqlCommand("Se...

value of type '1-dimensional array of byte ' cannot be converted to 'Byte'
Hi  Im having some issues with a , i thought, simple app to save an image as binary into a db.  I am using the code below which was originally C and ive tried to convert to VB.  I keep getting the error ''value of type '1-dimensional array of byte ' cannot be converted to 'Byte' '' on this line of codeDim rowsaffected As Integer = SaveToDB(imgname, imgbinarydata, imgcontenttype)   my full code is this:    Private Sub SaveToDB(ByVal imgname As String, ByVal imgbin As Byte, ByVal imgcontenttype As String) as integer ...

Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'
I would like to create a function so that I can pass a string and it will return me the binary value, I will use this later in other parts of the script but I am getting an error that I don't understand. Private Function ConvertToMD5(ByVal OldPassword As String) As Byte Dim NewPassword As String = "" 'The string we wish to encrypt Dim strPlainText As String = OldPassword 'The array of bytes that will contain the encrypted value of strPlainText Dim hashedDataBytes As Byte() 'The encoder class used to conv...

help with error 'value of type byte canot be converted to 1 dimensional array of byte'
hi i am getting an error with my code, it says 'value of type byte canot be converted to 1 dimensional array of byte' do you know why and how i can correct this error, the follwoing is my code. can anyone help me correct the error and let me know ow to solve it thanks for any help givenPublic Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequestDim myConnection As New Data.SqlClient.SqlConnection("ConnectionString") myConnection.Open()  Dim sql As String = "Select Image_Content from ImageGallery where Img_Id=@ImageId"Di...

Safe to convert int array into BYTE array?
It it safe to convert a *size_t* into a *BYTE* like I'm doing here? It seems to work fine. {code} size_t HELLO_WORLD[] = {0x48,0x65,0x6C,0x6C,0x6F,0x20,0x57,0x6F,0x72,0x6C,0x64}; size_t iSize = sizeof(HELLO_WORLD) / sizeof(size_t); BYTE * pArray = (BYTE *)malloc(iSize); for(size_t i = 0; i < iSize; i++) { pArray[i] = BYTE(HELLO_WORLD[i]); } {code} <Jason Goff> wrote in message news:400034@forums.embarcadero.com... > It it safe to convert a *size_t* into a *BYTE* like I'm doing here? It > seems to work fine. Yes, as long as none of the elemen...

Convertion of An Array Byte()
Dear all, Can anyone tell me how to do a selection through asp.net from an sql serv db 2000 to select an Image file?? Please do reply.. I am really lost. Thanks Alot Readiation Do you want to store, then read, an image file to SQL Server?? try http://www.dotnetspider.com/Technology/KBPages/558.aspx & http://odetocode.com/Articles/163.aspx Jerry Thanks for your immediate answer. I still am stuck on one thing though, When i did a file field and selected a file it is resulting as empty after doing the following code. If fileupload.PostedFile Is Nothing Then ...

"Decimal byte array constructor requires an array of length four containing valid decimal bytes"
For some reason, my AVG function in my SQL is failing on a decimal column. I have no idea why. When I have the same code, except change the column to be averaged from a decimal column to regular ole integer column, it works fine. I don't know. Here is the code: Function CreateDataSet(ByVal SQLstring As String) As DataSet Dim ds As DataSet = New DataSet Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source = c:\rynodb\aircraftprofile\aircraftprofile.mdb" Dim conn As OleDbConnection = New OleDbConnection(connS...

How to convert a byte[] array to a blob
I am dabbling in PowerBuilder for the first time so forgive me if this is a simple question, but I have been scouring the forums for an answer to this. How can I convert a byte[] array to a blob? I am calling a java web service from powerbuilder 10.5 using the .NET version of the web service proxy. The PB proxy is returning an ANY type but the actual type is a byte[] array. I see there are some methods for converting byte[] arrays to blobs in PB 11.5 (see link) but I need to do this in 10.5. http://groups.google.com/group/sybase.public.powerbuilder.general/browse_thread/threa...

String to Linq.Byte and Linq.Byte to String convertion?
 Hi, I have a field in my database as image type. I am using Linq to read/insert records.I am having little hard time inserting text(string) into the image field and then converting back to string.Do you have any suggestions? Thanks in advance,   The below example shows how to convert Linq Binary to byte If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("imageID")) Then Dim imageID = HttpContext.Current.Request.QueryString("imageID") Dim db As New AuctionsDataContext Dim result = From img In db.Image...

Convert byte array to image
I am trying to convert a byte array to an image for displaying on a page but currently receiving the following error; System.ArgumentException: Parameter is not valid   Stack trace; [ArgumentException: Parameter is not valid.]   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +1062667   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement) +12   _search.Page_Load(Object sender, EventArgs e) in D:\componentsuk1\search.aspx.vb:82   System.Web.UI.Control.On...

Web resources about - Converting Bitmap to Byte then Byte to Array:Arithmetic operation resulted in an overflow. - asp.net.drawinggdi

Resources last updated: 1/1/2016 5:11:18 AM