473,388 Members | 1,417 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,388 software developers and data experts.

MemoryStream Cast

Does anyone knows how to CAST this SQL Response into a MemoryStream ??
When executing below code an error message says "Specified cast is not valid"

I need to put this into MemoryStream to use it into imgPhoto
Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)

Dim dbConn, SQLStmt, dbComm, dbRead, img
Dim myStream 'As New MemoryStream
dbConn = New
System.Data.OleDb.OleDbConnection(Application("con nection_string"))
dbConn.Open()
img = "picture1"
SQLStmt = "SELECT figura "
SQLStmt = SQLStmt & "FROM figura_tb "
SQLStmt = SQLStmt & "Where nome = "
SQLStmt = SQLStmt & "'"
SQLStmt = SQLStmt & img
SQLStmt = SQLStmt & "'"
dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
dbRead = dbComm.ExecuteReader()
If dbRead.read() Then
myStream = dbRead("figura")
End If
Nov 18 '05 #1
7 2383
google is your true dear friend...

Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial
catalog=master;
user id=sa;password=mms")
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select * from
SampleImageTable")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim memorybits As New MemoryStream(bits)

http://www.dotnetspider.com/Technology/KBPages/558.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:7F**********************************@microsof t.com...
Does anyone knows how to CAST this SQL Response into a MemoryStream ??
When executing below code an error message says "Specified cast is not valid"
I need to put this into MemoryStream to use it into imgPhoto
Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)

Dim dbConn, SQLStmt, dbComm, dbRead, img
Dim myStream 'As New MemoryStream
dbConn = New
System.Data.OleDb.OleDbConnection(Application("con nection_string"))
dbConn.Open()
img = "picture1"
SQLStmt = "SELECT figura "
SQLStmt = SQLStmt & "FROM figura_tb "
SQLStmt = SQLStmt & "Where nome = "
SQLStmt = SQLStmt & "'"
SQLStmt = SQLStmt & img
SQLStmt = SQLStmt & "'"
dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
dbRead = dbComm.ExecuteReader()
If dbRead.read() Then
myStream = dbRead("figura")
End If

Nov 18 '05 #2
Dear Hermit Dave,

Thanks. It has worked fine.

But now, I need to solve other problem.
Using below code I can read transparet gif from SQL but at screen it shows
transparent area as black. Can you help me?
Imports System.io
Public Class teste
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("picture1", 50)
End Sub
Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura from
figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

End Class


"Hermit Dave" wrote:
google is your true dear friend...

Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial
catalog=master;
user id=sa;password=mms")
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select * from
SampleImageTable")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim memorybits As New MemoryStream(bits)

http://www.dotnetspider.com/Technology/KBPages/558.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:7F**********************************@microsof t.com...
Does anyone knows how to CAST this SQL Response into a MemoryStream ??
When executing below code an error message says "Specified cast is not

valid"

I need to put this into MemoryStream to use it into imgPhoto
Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)

Dim dbConn, SQLStmt, dbComm, dbRead, img
Dim myStream 'As New MemoryStream
dbConn = New
System.Data.OleDb.OleDbConnection(Application("con nection_string"))
dbConn.Open()
img = "picture1"
SQLStmt = "SELECT figura "
SQLStmt = SQLStmt & "FROM figura_tb "
SQLStmt = SQLStmt & "Where nome = "
SQLStmt = SQLStmt & "'"
SQLStmt = SQLStmt & img
SQLStmt = SQLStmt & "'"
dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
dbRead = dbComm.ExecuteReader()
If dbRead.read() Then
myStream = dbRead("figura")
End If


Nov 18 '05 #3
have a look at this link.. the code is in c# but i gave you the link which
does the code conversion so play with it.

http://www.c-sharpcorner.com/Code/20...nailImages.asp

http://dotnetjunkies.com/Newsgroups/.../19/65540.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:68**********************************@microsof t.com...
Dear Hermit Dave,

Thanks. It has worked fine.

But now, I need to solve other problem.
Using below code I can read transparet gif from SQL but at screen it shows
transparent area as black. Can you help me?
Imports System.io
Public Class teste
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("picture1", 50)
End Sub
Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura from figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

End Class


"Hermit Dave" wrote:
google is your true dear friend...

Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial
catalog=master;
user id=sa;password=mms")
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select * from
SampleImageTable")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim memorybits As New MemoryStream(bits)

http://www.dotnetspider.com/Technology/KBPages/558.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com > wrote in message

news:7F**********************************@microsof t.com...
Does anyone knows how to CAST this SQL Response into a MemoryStream ??
When executing below code an error message says "Specified cast is not

valid"

I need to put this into MemoryStream to use it into imgPhoto
Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)

Dim dbConn, SQLStmt, dbComm, dbRead, img
Dim myStream 'As New MemoryStream
dbConn = New
System.Data.OleDb.OleDbConnection(Application("con nection_string"))
dbConn.Open()
img = "picture1"
SQLStmt = "SELECT figura "
SQLStmt = SQLStmt & "FROM figura_tb "
SQLStmt = SQLStmt & "Where nome = "
SQLStmt = SQLStmt & "'"
SQLStmt = SQLStmt & img
SQLStmt = SQLStmt & "'"
dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
dbRead = dbComm.ExecuteReader()
If dbRead.read() Then
myStream = dbRead("figura")
End If


Nov 18 '05 #4
Robson,

Maybe you can use this rather old sample of my. It does not just show an
image, however you can position the thumbnail nice in a page.

I hope this helps?

Cor

\\\For the database the image database sample from the Resource kit.
\\\It needs 2 forms with a listbox, a picturebox and a label on form1
\\\webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("SELECT FileName, PictureID FROM Picture", conn)
Dim ds As New DataSet
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
da.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = exc.ToString
End Try
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebImage/WebForm2.aspx"
End Sub
///
\\\Webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", _
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///
Nov 18 '05 #5
Dear Hermit,

I'm trying to use this example since sep, 10 and I couldn't adjust my VB
code using your code.

Unfortunelly I'm not an expert VB programmer.

Can you or some other friend help me with below code?

Regards

Imports System.io

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("assinatura_promocao_1053", 150)
End Sub

Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura from
figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

"Hermit Dave" wrote:
have a look at this link.. the code is in c# but i gave you the link which
does the code conversion so play with it.

http://www.c-sharpcorner.com/Code/20...nailImages.asp

http://dotnetjunkies.com/Newsgroups/.../19/65540.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:68**********************************@microsof t.com...
Dear Hermit Dave,

Thanks. It has worked fine.

But now, I need to solve other problem.
Using below code I can read transparet gif from SQL but at screen it shows
transparent area as black. Can you help me?
Imports System.io
Public Class teste
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web

Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("picture1", 50)
End Sub
Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura

from
figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode =

Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

End Class


"Hermit Dave" wrote:
google is your true dear friend...

Dim con As New System.Data.SqlClient.SqlConnection("data source=mt5;initial catalog=master;
user id=sa;password=mms")
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select * from
SampleImageTable")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim memorybits As New MemoryStream(bits)

http://www.dotnetspider.com/Technology/KBPages/558.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com > wrote in message news:7F**********************************@microsof t.com... > Does anyone knows how to CAST this SQL Response into a MemoryStream ??
> When executing below code an error message says "Specified cast is not
valid"
>
> I need to put this into MemoryStream to use it into imgPhoto
> Dim imgPhoto As System.Drawing.Image =
> System.Drawing.Image.FromStream(myStream)
>
> Dim dbConn, SQLStmt, dbComm, dbRead, img
> Dim myStream 'As New MemoryStream
> dbConn = New
> System.Data.OleDb.OleDbConnection(Application("con nection_string"))
> dbConn.Open()
> img = "picture1"
> SQLStmt = "SELECT figura "
> SQLStmt = SQLStmt & "FROM figura_tb "
> SQLStmt = SQLStmt & "Where nome = "
> SQLStmt = SQLStmt & "'"
> SQLStmt = SQLStmt & img
> SQLStmt = SQLStmt & "'"
> dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
> dbRead = dbComm.ExecuteReader()
> If dbRead.read() Then
> myStream = dbRead("figura")
> End If


Nov 18 '05 #6
Dear Cor Ligthert,

Unfortunelly your example returns the same Color pallet problems when I try
to use transparent GIF. Transparent area shows itself as black and with other
GIFs color quality always degrade.

Can you help me with my original below code?

Regards
Robson Machado

Imports System.io

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("assinatura_promocao_1053", 150)
End Sub
Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura from
figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

"Cor Ligthert" wrote:
Robson,

Maybe you can use this rather old sample of my. It does not just show an
image, however you can position the thumbnail nice in a page.

I hope this helps?

Cor

\\\For the database the image database sample from the Resource kit.
\\\It needs 2 forms with a listbox, a picturebox and a label on form1
\\\webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("SELECT FileName, PictureID FROM Picture", conn)
Dim ds As New DataSet
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
da.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = exc.ToString
End Try
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebImage/WebForm2.aspx"
End Sub
///
\\\Webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", _
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///

Nov 18 '05 #7
Yes, I can't get transparent gifs but theres ono more thing that is related
to the same problem.
Any gifs are showed with poor quality because the color palet is not the
standard use by jpeg.

Thanks for your help.
I'll wait for your answer

Regards.
Robson Machado

"Hermit Dave" wrote:
i will have a look at it... i was under the impression that you had it
working only that you couldnt get transparent gifs anymore !!
is that correct ?

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado" <Ro*******************@discussions.microsoft.com >
wrote in message news:F1**********************************@microsof t.com...
Dear Hermit,

I'm trying to use this example since sep, 10 and I couldn't adjust my VB
code using your code.

Unfortunelly I'm not an expert VB programmer.

Can you or some other friend help me with below code?

Regards

Imports System.io

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ScaleStatic("assinatura_promocao_1053", 150)
End Sub

Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)

Dim con As New
System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
con.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("select figura from
figura_tb where nome='" & ImageName & "'")
cmd.Connection = con
cmd.CommandType = CommandType.Text
Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
Dim myStream As New MemoryStream(bits)

Dim imgPhoto As System.Drawing.Image =
System.Drawing.Image.FromStream(myStream)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = 0
Dim destHeight As Integer = 0

If imgPhoto.Width > imgPhoto.Height Then
destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
destWidth = intSize
Else
destHeight = intSize
destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
End If

Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
imgPhoto.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
grPhoto.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel)

bmPhoto.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif)
grPhoto.Dispose()
bmPhoto.Dispose()

End Sub

"Hermit Dave" wrote:
have a look at this link.. the code is in c# but i gave you the link
which
does the code conversion so play with it.

http://www.c-sharpcorner.com/Code/20...nailImages.asp

http://dotnetjunkies.com/Newsgroups/.../19/65540.aspx

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Robson Carvalho Machado"
<Ro*******************@discussions.microsoft.com >
wrote in message
news:68**********************************@microsof t.com...
> Dear Hermit Dave,
>
> Thanks. It has worked fine.
>
> But now, I need to solve other problem.
> Using below code I can read transparet gif from SQL but at screen it
> shows
> transparent area as black. Can you help me?
>
>
> Imports System.io
> Public Class teste
> Inherits System.Web.UI.Page
>
> #Region " Web Form Designer Generated Code "
>
> 'This call is required by the Web Form Designer.
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>
> End Sub
>
> 'NOTE: The following placeholder declaration is required by the Web
Form
> Designer.
> 'Do not delete or move it.
> Private designerPlaceholderDeclaration As System.Object
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> 'CODEGEN: This method call is required by the Web Form Designer
> 'Do not modify it using the code editor.
> InitializeComponent()
> End Sub
>
> #End Region
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> ScaleStatic("picture1", 50)
> End Sub
> Public Sub ScaleStatic(ByVal ImageName, ByVal intSize)
>
> Dim con As New
> System.Data.SqlClient.SqlConnection(Application("c onnection_string1"))
> con.Open()
> Dim cmd As New System.Data.SqlClient.SqlCommand("select figura
from
> figura_tb where nome='" & ImageName & "'")
> cmd.Connection = con
> cmd.CommandType = CommandType.Text
> Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
> Dim ds As New DataSet
> da.Fill(ds)
> Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0),
> Byte())
> Dim myStream As New MemoryStream(bits)
>
> Dim imgPhoto As System.Drawing.Image =
> System.Drawing.Image.FromStream(myStream)
> Dim sourceWidth As Integer = imgPhoto.Width
> Dim sourceHeight As Integer = imgPhoto.Height
> Dim sourceX As Integer = 0
> Dim sourceY As Integer = 0
> Dim destX As Integer = 0
> Dim destY As Integer = 0
> Dim destWidth As Integer = 0
> Dim destHeight As Integer = 0
>
> If imgPhoto.Width > imgPhoto.Height Then
> destHeight = ((intSize * 1.0 / sourceWidth) * sourceHeight)
> destWidth = intSize
> Else
> destHeight = intSize
> destWidth = ((intSize * 1.0 / sourceHeight) * sourceWidth)
> End If
>
> Dim bmPhoto As Bitmap = New Bitmap(destWidth, destHeight,
> imgPhoto.PixelFormat.Format24bppRgb)
> bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
> imgPhoto.VerticalResolution)
>
> Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
> grPhoto.CompositingQuality =
> Drawing.Drawing2D.CompositingQuality.HighQuality
> grPhoto.SmoothingMode =
Drawing.Drawing2D.SmoothingMode.HighQuality
> grPhoto.InterpolationMode =
> Drawing.Drawing2D.InterpolationMode.HighQualityBic ubic
>
> grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY,
> destWidth,
> destHeight), New Rectangle(sourceX, sourceY, sourceWidth,
> sourceHeight),
> GraphicsUnit.Pixel)
>
> bmPhoto.Save(Response.OutputStream,
> System.Drawing.Imaging.ImageFormat.Gif)
> grPhoto.Dispose()
> bmPhoto.Dispose()
>
> End Sub
>
> End Class
>
>
>
>
> "Hermit Dave" wrote:
>
> > google is your true dear friend...
> >
> > Dim con As New System.Data.SqlClient.SqlConnection("data
source=mt5;initial
> > catalog=master;
> > user id=sa;password=mms")
> > con.Open()
> > Dim cmd As New System.Data.SqlClient.SqlCommand("select * from
> > SampleImageTable")
> > cmd.Connection = con
> > cmd.CommandType = CommandType.Text
> > Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
> > Dim ds As New DataSet()
> > da.Fill(ds)
> > Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
> > Dim memorybits As New MemoryStream(bits)
> >
> > http://www.dotnetspider.com/Technology/KBPages/558.aspx
> >
> > --
> >
> > Regards,
> >
> > Hermit Dave
> > (http://hdave.blogspot.com)
> > "Robson Carvalho Machado"
<Ro*******************@discussions.microsoft.com >
> > wrote in message
news:7F**********************************@microsof t.com...
> > > Does anyone knows how to CAST this SQL Response into a MemoryStream
> > > ??
> > > When executing below code an error message says "Specified cast is
> > > not
> > valid"
> > >
> > > I need to put this into MemoryStream to use it into imgPhoto
> > > Dim imgPhoto As System.Drawing.Image =
> > > System.Drawing.Image.FromStream(myStream)
> > >
> > > Dim dbConn, SQLStmt, dbComm, dbRead, img
> > > Dim myStream 'As New MemoryStream
> > > dbConn = New
> > > System.Data.OleDb.OleDbConnection(Application("con nection_string"))
> > > dbConn.Open()
> > > img = "picture1"
> > > SQLStmt = "SELECT figura "
> > > SQLStmt = SQLStmt & "FROM figura_tb "
> > > SQLStmt = SQLStmt & "Where nome = "
> > > SQLStmt = SQLStmt & "'"
> > > SQLStmt = SQLStmt & img
> > > SQLStmt = SQLStmt & "'"
> > > dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt,
> > > dbConn)
> > > dbRead = dbComm.ExecuteReader()
> > > If dbRead.read() Then
> > > myStream = dbRead("figura")
> > > End If
> >
> >
> >


Nov 18 '05 #8

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

Similar topics

1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
3
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream();...
11
by: Rasmus Teglgaard | last post by:
Hi I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works I can't write dim mem as new memorystream( mem =...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
2
by: John A Grandy | last post by:
Dim ms As MemoryStream = New MemoryStream(100) ms.WriteByte("=") triggers invalid cast error ... but ms.WriteByte(Asc("="c))
13
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.