MemoryStream Cast

G

Guest

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("connection_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
 
H

Hermit Dave

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)
 
G

Guest

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("connection_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.HorizontalResolution,
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.HighQualityBicubic

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
 
C

Cor Ligthert

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
///
 
G

Guest

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("connection_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.HorizontalResolution,
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.HighQualityBicubic

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
 
G

Guest

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("connection_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.HorizontalResolution,
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.HighQualityBicubic

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
 
G

Guest

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
 
H

Hermit Dave

Rob,

The problem is that the code that i attached was for resizing jpegs and not
gifs.
For gifs you need to set the correct Pallet. and you are also limited to
8bit a pixel and 256 colors.

The first link i passed the second time around that has a project and to my
understanding there is nothing in the framework which helps you do
transparent gifs.
Download it... build it... dont understand it just yet.. use Reflector and
then decompile to your VB.NET code... see how its implemented.

You need to write the implementation for that.
The dotnetjunkies link has a discussion on whether transparent gifs was
possible and there was a msdn link in there that explained a great deal
pallete etc.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top