Hi,
1.
Why in the page when I click right button and want to try save an image I have the following message:
"The file type being save or retrieved has been blocked."
2.
How can I save images from web using VB.NET ?
I get images as:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# FormatURL(DataBinder.Eval(
Container.
DataItem, "fileID")) %>' />
Function FormatURL(ByVal strArgument) As String
Return ("readrealimage2.aspx?id="
& strArgument)
End Function
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ODB
C" %>
<HTML>
<HEAD>
<title>Read Record</title>
<script runat="server">
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strImageID As String = Request.QueryString("id")
' Create Instance of Connection and Command Object
Dim myConnection As OdbcConnection = New OdbcConnection(Configurati
onManager.
Connection
Strings("M
ySQLConnSt
ring1").To
String)
' Mark the Command as a SPROC
Dim cmdStr As String = "select * from bmmsfile WHERE fileid=" & strImageID
Dim myCommand As OdbcCommand = New OdbcCommand(cmdStr, myConnection)
Dim _image, _newimage As System.Drawing.Image
mycommand.CommandType = CommandType.Text
Try
myConnection.Open()
Dim myDataReader As OdbcDataReader
myDataReader = myCommand.ExecuteReader(Co
mmandBehav
ior.CloseC
onnection)
Do While (myDataReader.Read())
Response.ContentType = myDataReader.Item("fileid"
)
Dim bindata() As Byte = myDataReader.Item("data")
Dim Thumbnailbindata() As Byte = resizeImage(bindata)
Response.BinaryWrite(Thumb
nailbindat
a)
Loop
myConnection.Close()
Catch SQLexc As OdbcException
Response.Write("Error occured while Generating Data. Error is " & SQLexc.ToString())
End Try
End Sub
Public Function resizeImage(ByVal pic() As Byte) As Byte()
' Const THUMBNAIL_IMAGE_PATH As String = "C:\Thumbnails\Test.jpg"
Const maxWidth As Integer = 500
Const maxHeight As Integer = 500
Dim inp As New IntPtr
Dim imgHeight, imgWidth As Double
Try
Dim image As System.Drawing.Image = System.Drawing.Image.FromS
tream(New System.IO.MemoryStream(pic
))
Dim bm = New System.Drawing.Bitmap(imag
e)
Dim imgHres, imgVres As Single
'Added this for testing - usually 96 dpi for every picture
imgHres = bm.horizontalresolution
imgVres = bm.verticalresolution
imgHeight = bm.Height
imgWidth = bm.Width
If imgWidth > maxWidth Or imgHeight > maxHeight Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double
If deltaHeight > deltaWidth Then
'Scale by the height
scaleFactor = maxHeight / imgHeight
Else
'Scale by the Width
scaleFactor = maxWidth / imgWidth
End If
imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If
Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim imgbmp As System.Drawing.Image = bm.GetThumbnailImage(w, h, Nothing, inp)
Dim ms As New System.IO.MemoryStream
imgbmp.Save(ms, System.Drawing.Imaging.Ima
geFormat.J
peg)
Dim b(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(b, 0, ms.Length)
Return b
'bitmap.Save(THUMBNAIL_IMA
GE_PATH, Imaging.ImageFormat.Jpeg)
Catch ex As Exception
End Try
End Function 'resizeImage
Public Function ConvertTo(ByVal ImageArray As Byte()) As System.Drawing.Image
'<summary>
' Convert a SQL Server database byte Array (byte array) to a Image.
'</summary>
Dim objMS As System.IO.MemoryStream
Dim objImage As System.Drawing.Image
objMS = New System.IO.MemoryStream(Ima
geArray)
' Convert the database byte array to a System Drawing Image.
ConvertTo = System.Drawing.Image.FromS
tream(objM
S)
End Function
</script>
</HEAD>
<body style="font: 10pt verdana">
<form runat="server" id="Form1">
</form>
</body>
</HTML>