Converting Bitmap to Byte - asp.net

I have the following code:
Private Function SizeLogo(ByVal logoBytes As Byte()) As Byte()
Using originalMemStream As MemoryStream = New MemoryStream(logoBytes),
originalImage As System.Drawing.Image = System.Drawing.Image.FromStream(originalMemStream)
Dim width As Integer = originalImage.Width
Dim height As Integer = originalImage.Height
Dim targetWidth As Integer = 300
Dim targetHeight As Integer = 200
Dim newHeight As Integer = 0
Dim newWidth As Integer = 0
Using oBitmap As New Bitmap(300, 200)
Using oGraphic As Graphics = Graphics.FromImage(oBitmap)
Using oBrush As New SolidBrush(Color.White)
oGraphic.FillRectangle(oBrush, 0, 0, targetWidth, targetHeight)
End Using
Using oProduto As System.Drawing.Image = Drawing.Image.FromStream(originalMemStream)
Dim targetRatio As Decimal = CDec(targetWidth) / CDec(targetHeight)
Dim imageRatio As Decimal = CDec(width) / CDec(height)
If targetRatio > imageRatio Then
newHeight = targetHeight
newWidth = CInt(Math.Floor(imageRatio * CDec(targetHeight)))
Else
newHeight = CInt(Math.Floor(CDec(targetWidth) / imageRatio))
newWidth = targetWidth
End If
If newWidth > targetWidth Then newWidth = targetWidth
If newHeight > targetHeight Then newHeight = targetHeight
oGraphic.DrawImage(oProduto, 0, 0, newWidth, newHeight)
**Using thumbImage As Drawing.Image = oGraphic. .GetThumbnailImage(targetWidth, targetHeight, Nothing, IntPtr.Zero),
outMemStream As MemoryStream = New MemoryStream()
thumbImage.Save(outMemStream, ImageFormat.Jpeg)
Return outMemStream.ToArray()
End Using**
End Using
End Using
End Using
End Using
End Function
What I am trying to do is
Create 300width and 200height white bitmap
Draw and center image that was uploaded into center
My problem is I am unable to save the created image. Is this a conversion issue? I've highlighted code where I think I am failing at.

You will need to save the oBitmap. Also if you want to center the image within the 300 x 200 bitmap you will need to pass calculated x, y to oGraphic.DrawImage instead of passing 0, 0
Here is the code (note that I've changed the solid brush color to Red for clarity; make sure to change that to white for your case)
Private Function SizeLogo(ByVal logoBytes As Byte()) As Byte()
Using originalMemStream As MemoryStream = New MemoryStream(logoBytes),
originalImage As System.Drawing.Image = System.Drawing.Image.FromStream(originalMemStream)
Dim width As Integer = originalImage.Width
Dim height As Integer = originalImage.Height
Dim targetWidth As Integer = 300
Dim targetHeight As Integer = 200
Dim newHeight As Integer = 0
Dim newWidth As Integer = 0
Using oBitmap As New Bitmap(300, 200)
Using oGraphic As Graphics = Graphics.FromImage(oBitmap)
Using oBrush As New SolidBrush(Color.Red)
oGraphic.FillRectangle(oBrush, 0, 0, targetWidth, targetHeight)
End Using
Using oProduto As System.Drawing.Image = Drawing.Image.FromStream(originalMemStream)
Dim targetRatio As Decimal = CDec(targetWidth) / CDec(targetHeight)
Dim imageRatio As Decimal = CDec(width) / CDec(height)
If targetRatio > imageRatio Then
newHeight = targetHeight
newWidth = CInt(Math.Floor(imageRatio * CDec(targetHeight)))
Else
newHeight = CInt(Math.Floor(CDec(targetWidth) / imageRatio))
newWidth = targetWidth
End If
If newWidth > targetWidth Then newWidth = targetWidth
If newHeight > targetHeight Then newHeight = targetHeight
Dim x As Integer = 0
Dim y As Integer = 0
If (newWidth < targetWidth) Then
x = (targetWidth - newWidth) / 2
End If
If (newHeight < targetHeight) Then
y = (targetHeight - newHeight) / 2
End If
oGraphic.DrawImage(oProduto, x, y, newWidth, newHeight)
'uncomment this to save to file (just to test output)
'oBitmap.Save("D:\penguins.bmp")
Dim outMemStream As MemoryStream = New MemoryStream()
oBitmap.Save(outMemStream, ImageFormat.Jpeg)
Return outMemStream.GetBuffer()
End Using
End Using
End Using
End Using
End Function
the output image will look like

Related

Argument 'expression' cannot be converted to type 'DBNull'

I have an app by vb for taking a list of doneators in Dvg and have about 17 forum and local mssql db and using if not is dbnull expression and by pressing the button some calculation happened and the answer appears in labels on the fourm.. But A strange problem happened when I run this app by the visual studio 2017 it run perfectly and there is no error.. But when converting it to exe file and setup it the following error appear when pressing this button " Argument 'expression' cannot be converted to type 'DBNull'
" although it's not appear when run it with visual studio.. What could the problem be..?
here is the code but i don't think the problem in it because it works fine in visual studio
its long code so i will put a part of it and the rest is repeated for 12 months and 10 yearsthis is pic of the error
If Form1.ComboBox1.Text = "2018" Then
' total kfalat
If TextBox1.Text = "1" Then
Form5.total.Text = Form1.Table1DataGridView.Rows.Count - 1
Dim HoN As Integer = 0
' total done
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "1" Then
HoN = HoN + 1
End If
End If
End If
End If
Next
Form5.done.Text = HoN.ToString
'undone
Form5.notdone.Text = Val(Form5.total.Text) - Val(Form5.done.Text)
'asbab 3adam alta7seel
'lm yarod
Dim HoN11 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "لم يرد" Then
HoN11 = HoN11 + 1
End If
End If
End If
End If
Next
Form5.Label69.Text = HoN11.ToString
'lmo2agal
Dim HoN12 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "مؤجل" Then
HoN12 = HoN12 + 1
End If
End If
End If
End If
Next
Form5.Label67.Text = HoN12.ToString
'سيحضر
Dim HoN13 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "سيحضر " Then
HoN13 = HoN13 + 1
End If
End If
End If
End If
Next
Form5.Label65.Text = HoN13.ToString
Dim HoN10 As Integer = 0
Dim money As Integer = 0
' astmarat gdeda
For Each gvRow As DataGridViewRow In Form1.Table1DataGridView.Rows
If Not IsDBNull(gvRow.Cells(8).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(8).Value) AndAlso Val(gvRow.Cells(8).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(15).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(15).Value > 0 Then
HoN10 = HoN10 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
money = money + Val(gvRow.Cells(15).Value)
End If
End If
End If
End If
Next
Form5.Label59.Text = HoN10.ToString
Form5.Label58.Text = money.ToString
Dim orphensmoney1 As Integer = 0
Dim HoN1 As Integer = 0
Dim familiesmoney1 As Integer = 0
Dim HoN2 As Integer = 0
Dim sicksmoney1 As Integer = 0
Dim HoN3 As Integer = 0
Dim studentsmoney1 As Integer = 0
Dim HoN4 As Integer = 0
' alkafalat almotwagda belf3l
For Each gvRow As DataGridViewRow In Form1.Table1DataGridView.Rows
If Not IsDBNull(gvRow.Cells(19).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(19).Value = True Then
HoN1 = HoN1 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
orphensmoney1 = orphensmoney1 + Val(gvRow.Cells(23).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(20).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(20).Value = True Then
HoN2 = HoN2 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
familiesmoney1 = familiesmoney1 + Val(gvRow.Cells(24).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(21).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(21).Value = True Then
HoN3 = HoN3 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
sicksmoney1 = sicksmoney1 + Val(gvRow.Cells(25).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(22).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(22).Value = True Then
HoN4 = HoN4 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
studentsmoney1 = studentsmoney1 + Val(gvRow.Cells(26).Value)
End If
End If
Next
Form5.Label11.Text = HoN1.ToString
Form5.Label10.Text = orphensmoney1.ToString
Form5.Label14.Text = HoN2.ToString
Form5.Label13.Text = familiesmoney1.ToString
Form5.Label17.Text = HoN3.ToString
Form5.Label16.Text = sicksmoney1.ToString
Form5.Label20.Text = HoN4.ToString
Form5.Label19.Text = studentsmoney1.ToString
Form5.Label8.Text = (Val(HoN1) + Val(HoN2) + Val(HoN3) + Val(HoN4)).ToString
Form5.Label9.Text = (Val(orphensmoney1) + Val(familiesmoney1) + Val(sicksmoney1) + Val(studentsmoney1)).ToString
Dim HoN5 As Integer = 0
Dim ORPHENS As Integer = 0
Dim HoN6 As Integer = 0
Dim families As Integer = 0
Dim HoN7 As Integer = 0
Dim sicks As Integer = 0
Dim HoN8 As Integer = 0
Dim students As Integer = 0
' almo7asal fe kol band
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(14).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(14).Value > 0 Then
HoN5 = HoN5 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
ORPHENS = ORPHENS + Val(gvRow.Cells(14).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(16).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(16).Value > 0 Then
HoN6 = HoN6 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
families = families + Val(gvRow.Cells(16).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(18).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(18).Value > 0 Then
HoN7 = HoN7 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
sicks = sicks + Val(gvRow.Cells(18).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(20).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(20).Value > 0 Then
HoN8 = HoN8 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
students = students + Val(gvRow.Cells(20).Value)
End If
End If
End If
End If
Next
Form5.Label32.Text = HoN5.ToString
Form5.Label31.Text = ORPHENS.ToString
Form5.Label29.Text = HoN6.ToString
Form5.Label28.Text = families.ToString
Form5.Label26.Text = HoN7.ToString
Form5.Label25.Text = sicks.ToString
Form5.Label23.Text = HoN8.ToString
Form5.Label22.Text = students.ToString
Form5.Label35.Text = Val(HoN8.ToString) + Val(HoN7.ToString) + Val(HoN6.ToString) + Val(HoN5.ToString)
Form5.Label34.Text = Val(ORPHENS.ToString) + Val(families.ToString) + Val(sicks.ToString) + Val(students.ToString)
Form5.Label53.Text = Val(Form5.Label8.Text) - Val(Form5.Label35.Text)
Form5.Label52.Text = Val(Form5.Label9.Text) - Val(Form5.Label34.Text)
Form5.Label50.Text = Val(Form5.Label11.Text) - Val(Form5.Label32.Text)
Form5.Label49.Text = Val(Form5.Label10.Text) - Val(Form5.Label31.Text)
Form5.Label47.Text = Val(Form5.Label14.Text) - Val(Form5.Label29.Text)
Form5.Label46.Text = Val(Form5.Label13.Text) - Val(Form5.Label28.Text)
Form5.Label44.Text = Val(Form5.Label17.Text) - Val(Form5.Label26.Text)
Form5.Label43.Text = Val(Form5.Label16.Text) - Val(Form5.Label25.Text)
Form5.Label41.Text = Val(Form5.Label20.Text) - Val(Form5.Label23.Text)
Form5.Label40.Text = Val(Form5.Label19.Text) - Val(Form5.Label22.Text)
End If

upload image as 24bit pixel format

I use an asp file uploader to upload a bitmap image. The original image is saved in hard disk in 24 bit format but the uploader converted it to 32 bit and subsequently gives wrong result in my web application
How can I upload image as its original 24 bit?
Dim face As search_eng.face
face = New face() ' initialize object (face)
If (Not System.IO.Directory.Exists(getpath() + "\pics\")) Then
System.IO.Directory.CreateDirectory(getpath() + "\pics\")
End If
Dim i = 0
While System.IO.File.Exists(getpath() + "\pics\" + i.ToString + ".jpg")
i += 1
End While
FileUpload1.SaveAs(getpath() + "\pics\" + i.ToString + ".jpg")
Image1.ImageUrl = "\pics\" + i.ToString + ".jpg"
Dim fs As System.IO.FileStream ' define a file stream
fs = New IO.FileStream(getpath() + "\pics\" + i.ToString + ".jpg", IO.FileMode.Open, IO.FileAccess.Read) ' open for read
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromStream(fs)
fs.Dispose()
Dim btimg As System.Drawing.Bitmap
Dim Original_img As System.Drawing.Bitmap
btimg = New Drawing.Bitmap(img, img.Width, img.Height)
Original_img = New Drawing.Bitmap(img, img.Width, img.Height)
img = btimg
Original_img = btimg
Original_img.Save("d:\after_open_Web.bmp")

ASP.Net - Mathematic operations and percentages - VB

I have a table as below which I am populating with the number of 'Pass' and 'Fail' records in each column. I am then trying to work out the failure rate for each column and an average failure rate for the 3 'IR Room' devices and the 3 'Employee Exit' devices.
I am populating the labels using the code at the bottom of this post, however I have 2 issues:-
I am getting an 'Input string was not in the correct format' error
on line 86 (Dim irAverageFailureRate as String)
I cannot help but think there is a better way to do this. I'm
new to ASP/VB so any advice on how to improve this would be
gratefully received.
Sub getYesterdayStats()
Dim con As OleDbConnection
Dim cmdClockings As OleDbDataAdapter
Dim dsClockings As New DataSet
Dim yesterday As String = DateTime.Today.AddDays(-1).ToString("yyyy/MM/dd")
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=\\XXX.XX.XXX.XX\XXXX.mdb;Jet OLEDB:Database Password=XXXXX;")
cmdClockings = New OleDbDataAdapter("SELECT efrLog.PID, efrLog.LType, efrLog.LDName from efrLog WHERE LDate = '" & yesterday & "'", con)
cmdClockings.Fill(dsClockings, "Clockings")
' Get pass and fail count for each device
Dim ir1Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 1'").Count
Dim ir1Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 1'").Count
Dim ir2Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 2'").Count
Dim ir2Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 2'").Count
Dim ir3Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'IR Room 3'").Count
Dim ir3Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'IR Room 3'").Count
Dim ee1Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 1'").Count
Dim ee1Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 1'").Count
Dim ee2Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 2'").Count
Dim ee2Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 2'").Count
Dim ee3Pass As Integer = dsClockings.Tables("Clockings").Select("LType = 'PASS' AND LDName = 'Employee Exit 3'").Count
Dim ee3Fail As Integer = dsClockings.Tables("Clockings").Select("LType = 'DENY' AND LDName = 'Employee Exit 3'").Count
' Calculate totals for each device
Dim ir1total As Integer = ir1Pass + ir1Fail
Dim ir2total As Integer = ir2Pass + ir2Fail
Dim ir3total As Integer = ir3Pass + ir3Fail
Dim ee1total As Integer = ee1Pass + ee1Fail
Dim ee2total As Integer = ee2Pass + ee2Fail
Dim ee3total As Integer = ee3Pass + ee3Fail
' Calcualte failure rate for each device
Dim ir1failureRate As String = Format((ir1Fail / ir1Pass) * 100, "0.00")
Dim ir2failureRate As String = Format((ir2Fail / ir2Pass) * 100, "0.00")
Dim ir3failureRate As String = Format((ir3Fail / ir3Pass) * 100, "0.00")
Dim ee1failureRate As String = Format((ee1Fail / ee1Pass) * 100, "0.00")
Dim ee2failureRate As String = Format((ee2Fail / ee2Pass) * 100, "0.00")
Dim ee3failureRate As String = Format((ee3Fail / ee3Pass) * 100, "0.00")
' Calculate average failure rate for IR and EE devices
Dim irAverageFailureRate As String = Format((Format((ir1Fail / ir1Pass) * 100, "0.00") + Format((ir2Fail / ir2Pass) * 100, "0.00") + Format((ir3Fail / ir3Pass) * 100, "0.00")) / 3, "0.00")
Dim eeAverageFailureRate As String = Format((Format((ee1Fail / ee1Pass) * 100, "0.00") + Format((ee2Fail / ee2Pass) * 100, "0.00") + Format((ee3Fail / ee3Pass) * 100, "0.00")) / 3, "0.00")
' Assign values to labels
lblIR1pass.Text = ir1Pass
lblIR1fail.Text = ir1Fail
lblIR2pass.Text = ir2Pass
lblIR2fail.Text = ir2Fail
lblIR3pass.Text = ir3Pass
lblIR3fail.Text = ir3Fail
lblEE1pass.Text = ee1Pass
lblEE1fail.Text = ee1Fail
lblEE2pass.Text = ee2Pass
lblEE2fail.Text = ee2Fail
lblEE3pass.Text = ee3Pass
lblEE3fail.Text = ee3Fail
lblIR1tot.Text = ir1total
lblIR2tot.Text = ir2total
lblIR3tot.Text = ir3total
lblEE1tot.Text = ee1total
lblEE2tot.Text = ee2total
lblEE3tot.Text = ee3total
lblIR1fr.Text = ir1failureRate & "%"
lblIR2fr.Text = ir2failureRate & "%"
lblIR3fr.Text = ir3failureRate & "%"
lblEE1fr.Text = ee1failureRate & "%"
lblEE2fr.Text = ee2failureRate & "%"
lblEE3fr.Text = ee3failureRate & "%"
lblIRafr.Text = irAverageFailureRate & "%"
lblEEafr.Text = eeAverageFailureRate & "%"
End Sub
There's a lot of unnecessary Formats in that line.
Dim irAverageFailureRate As String = ((CDbl(ir1Fail) / ir1Pass) + (CDbl(ir2Fail) / ir2Pass) + (CDbl(ir3Fail) / ir3Pass) / 3).ToString("P")
I got the "P" for percentage from Standard Numeric Format Strings.

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. imp

hye....its kinda urgent...
iam using a temp folder to store images for the preview and when i try to save them a System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+ erreo is reported...
If FileUpload1.HasFile Then
Dim path As String = Server.MapPath("./TempImages/")
Dim oFileInfo As New FileInfo(FileUpload1.PostedFile.FileName)
Dim fileName As String = oFileInfo.Name
Dim fullFileName As String = path + "\\" + fileName
Dim imagePath As String = "TempImages/" + fileName
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
FileUpload1.PostedFile.SaveAs(fullFileName)
Image1.ImageUrl = imagePath
this is what iam doing and to save
If (FileUpload1.HasFile) Then
Dim strextension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
If (strextension.ToUpper() = ".JPG") Then
Dim imagetoberezised As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
Dim imageheight As Integer = imagetoberezised.Height
Dim imagewidth As Integer = imagetoberezised.Width
Dim maxheight As Integer = 120
Dim maxwidth As Integer = 80
imageheight = (imagewidth * maxheight) / imageheight
imagewidth = maxheight
If imageheight > maxheight Then
imageheight = (imagewidth * maxheight) / imageheight
imagewidth = maxheight
End If
Dim bitmap As New Bitmap(imagetoberezised, imagewidth, imageheight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim img As Byte() = New Byte(stream.Length) {}
stream.Read(img, 0, img.Length)
Dim conn5 As New SqlConnection
conn5 = New SqlConnection
conn5.ConnectionString = "Server=aa-pc;Database=abcc;Uid=sa;Pwd=sa#123;"
Try
conn5.Open()
Dim sql As String = "update abcset abc_IMAGE = #eimg where PART_NO=" & Val(Part_List.SelectedValue)
Dim cmd5 As SqlCommand = New SqlCommand(sql, conn5)
Dim uploadimage As New SqlParameter("#eimg", SqlDbType.Image, img.Length)
uploadimage.Value = img
cmd5.Parameters.Add(uploadimage)
Dim id As Integer = Convert.ToInt32(cmd5.ExecuteScalar())
Catch
MsgBox("error")
Finally
conn5.Close()
End Try
End If
End If
it works well whilw we run it locally but not on server..
Try this, it work for me:
Dim path As String = Server.MapPath("~/TempImages")
Dim FileInfo As New IO.FileInfo(FileUpload1.PostedFile.FileName)
Dim fileName As String = FileInfo.Name
'sets the image path
Dim fullFileName As String = path + "\" + fileName
Dim imagePath As String = "TempImages/" + fileName
'If Not Directory.Exists(path) Then
If Not System.IO.Directory.Exists("directory path") Then
System.IO.Directory.Exists("directory path")
End If
FileUpload1.PostedFile.SaveAs(fullFileName)
Image1.ImageUrl = imagePath

asp:Table change row color programmatically

I am learning .net and building a table from code behind and attempting to change alternate row colors.
I have it working but at he moment only using Drawing.Color when I would like to use a hexidecimal value, is there a way of doing this?
Here is the code thats doing it at the moment:
If j Mod 2 = 1 Then
r.BackColor = Drawing.Color.Aquamarine
'Table1.Rows(j).Cells(i).CssClass = "odd"
Else
r.BackColor = Drawing.Color.Blue
'Table1.Rows(j).Cells(i).CssClass = "even"
End If
Here is full code:
' declare variables
Dim numrows As Integer
Dim numcells As Integer
Dim i As Integer
Dim j As Integer
Dim r As TableRow
Dim c As TableCell
Dim hc As TableHeaderCell
'Dim datepickJS As String
' generate rows and cells
numrows = CaseTotal
' column count
numcells = 7
r = New TableRow()
c = New TableCell()
'' validation creation
'Dim newValidatorSummary As New ValidationSummary()
'newValidatorSummary.CssClass = "form-error"
'c.Controls.Add(newValidatorSummary)
'c.ColumnSpan = 5
'r.Cells.Add(c)
'Table1.Rows.Add(r)
r = New TableRow()
' header row and titles
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Case Ref"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Name"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Company"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Last Updated"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Order Date"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl(" "))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl(" "))
r.Cells.Add(hc)
Table1.Rows.Add(r)
r.BackColor = Drawing.Color.AntiqueWhite
For j = 0 To numrows - 1
r = New TableRow()
'Table1.Rows(j).Cells(i).CssClass = "tablehead"
'row data
Dim ClientName, Company As String
Dim OrderDate As Date
ClientName = dsCases.Tables("CaseList").Rows(j).Item("firstname") & " " & dsCases.Tables("CaseList").Rows(j).Item("lastname")
Company = dsCases.Tables("CaseList").Rows(j).Item("company")
OrderDate = dsCases.Tables("CaseList").Rows(j).Item("CaseSent")
'create details button
Dim btnDetails As New Button()
btnDetails.Text = "Details"
btnDetails.ID = "btnDetails"
btnDetails.Width = "60"
btnDetails.Font.Size = "8"
'AddHandler btnSubmit.Click, AddressOf sendDetails_Click
'create schedule button
Dim btnSchedule As New Button()
btnSchedule.Text = "Schedule"
btnSchedule.ID = "btnSchedule"
btnSchedule.Width = "60"
btnSchedule.Font.Size = "8"
'AddHandler btnSubmit.Click, AddressOf sendDetails_Click
For i = 0 To numcells - 1
Dim iMod As Integer
iMod = j Mod 2
c = New TableCell()
If i = 0 Then
c.Controls.Add(New LiteralControl(iMod & " "))
c.Controls.Add(New LiteralControl(UserId))
ElseIf i = 1 Then
c.Controls.Add(New LiteralControl(ClientName))
ElseIf i = 2 Then
c.Controls.Add(New LiteralControl(Company))
ElseIf i = 3 Then
ElseIf i = 4 Then
c.Controls.Add(New LiteralControl(FormatDateTime(OrderDate, DateFormat.ShortDate)))
ElseIf i = 5 Then
c.Controls.Add(btnDetails)
ElseIf i = 6 Then
c.Controls.Add(btnSchedule)
End If
r.Cells.Add(c)
Next i
Table1.Rows.Add(r)
If j Mod 2 = 1 Then
r.BackColor = Drawing.Color.Aquamarine
'Table1.Rows(j).Cells(i).CssClass = "odd"
Else
r.BackColor = Drawing.Color.Blue
'Table1.Rows(j).Cells(i).CssClass = "even"
End If
Next j
r = New TableRow()
' footer row
c = New TableCell()
c.ColumnSpan = 7
c.HorizontalAlign = HorizontalAlign.Right
r.Cells.Add(c)
Table1.Rows.Add(r)
Thanks for any help.
J.
You can use the ColorTranslator class like this:
r.BackColor = ColorTranslator.FromHtml("#0000FF")
Documentation here: http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx

Resources