Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > LINQ and blobs

Reply
Thread Tools

LINQ and blobs

 
 
James Page
Guest
Posts: n/a
 
      09-25-2008
Hi all another LINQ question!!

to retrieve and display sql varbinary images I currently use the following
code:

Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO



Partial Class ShowPicture
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim PictureID As Integer =
Convert.ToInt32(Request.QueryString("PictureID"))

'Connect to the database and bring back the image contents & MIME
type for the specified picture
Using myConnection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("ImageGalleryConnectionString").ConnectionStrin g)

Const SQL As String = "SELECT [MIMEType], [ImageData] FROM
[Pictures] WHERE [PictureID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@PictureID", PictureID)

myConnection.Open()

Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
Response.ContentType = myReader("MIMEType").ToString()
Response.BinaryWrite(myReader("ImageData"))


End If
myReader.Close()
myConnection.Close()
End Using
End Sub


End Class

I'm now trying to use LINQ to replace the sql elements. Can anyone help me
convert the above using LINQ?
 
Reply With Quote
 
 
 
 
James Page
Guest
Posts: n/a
 
      09-25-2008
After a bit of digging I've found a solution let me know what you guys think!

Here's the new code - including a method to proportionaly display the
resultant image:

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Data.Linq


Partial Class showPicture
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim imageID As Integer =
Convert.ToInt32(Request.QueryString("PictureID"))

Dim db As New DataClassesDataContext()

Dim query As Binary = (From x In db.pictures Where x.ID = imageID
Select x.image).Single()

Dim imgContent As Byte() = query.ToArray()
Response.ContentType = ("image/jpeg")

'set the size of the desired thumbnail in px
Dim thumbWidth As Integer = 200
Dim thumbHeight As Integer = 200

Dim s As New MemoryStream(imgContent)


Dim image1 As New Bitmap(s)

Dim bmpOut As System.Drawing.Bitmap

Try
Dim bmpIn As New Bitmap(s)
Dim bmpFormat As ImageFormat = bmpIn.RawFormat
Dim calcRatio As Decimal
Dim thumbNewWidth As Integer = 0
Dim thumbNewHeight As Integer = 0

If bmpIn.Width > bmpIn.Height Then
calcRatio = CDec(thumbWidth) / bmpIn.Width
thumbNewWidth = thumbWidth
Dim thumbHeightTemp1 As Decimal = bmpIn.Height * calcRatio
thumbNewHeight = CInt(thumbHeightTemp1)
Else
calcRatio = CDec(thumbHeight) / bmpIn.Height
thumbNewHeight = thumbHeight
Dim thumbWidthTemp2 As Decimal = bmpIn.Width * calcRatio
thumbNewWidth = CInt(thumbWidthTemp2)
End If

bmpOut = New Bitmap(thumbNewWidth, thumbNewHeight)
Dim g As Graphics = Graphics.FromImage(bmpOut)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic
g.FillRectangle(Brushes.White, 0, 0, thumbNewWidth,
thumbNewHeight)
g.DrawImage(bmpIn, 0, 0, thumbNewWidth, thumbNewHeight)

bmpIn.Dispose()
Catch
Return
End Try

Try
bmpOut.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
Catch
Finally
bmpOut.Dispose()
End Try
End Sub
End Class


This seems to work!
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      09-26-2008
why did you switch to linq for this query?

linq much less efficient for a simple query like this. at runtime linq
will read the parse tree generated from the query, then convert it to a
sql string, then call the same code you wrote in the first place.


-- bruce (sqlwork.com)




James Page wrote:
> After a bit of digging I've found a solution let me know what you guys think!
>
> Here's the new code - including a method to proportionaly display the
> resultant image:
>
> Imports System.Data.SqlClient
> Imports System.IO
> Imports System.Drawing
> Imports System.Drawing.Imaging
> Imports System.Data.Linq
>
>
> Partial Class showPicture
> Inherits System.Web.UI.Page
>
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim imageID As Integer =
> Convert.ToInt32(Request.QueryString("PictureID"))
>
> Dim db As New DataClassesDataContext()
>
> Dim query As Binary = (From x In db.pictures Where x.ID = imageID
> Select x.image).Single()
>
> Dim imgContent As Byte() = query.ToArray()
> Response.ContentType = ("image/jpeg")
>
> 'set the size of the desired thumbnail in px
> Dim thumbWidth As Integer = 200
> Dim thumbHeight As Integer = 200
>
> Dim s As New MemoryStream(imgContent)
>
>
> Dim image1 As New Bitmap(s)
>
> Dim bmpOut As System.Drawing.Bitmap
>
> Try
> Dim bmpIn As New Bitmap(s)
> Dim bmpFormat As ImageFormat = bmpIn.RawFormat
> Dim calcRatio As Decimal
> Dim thumbNewWidth As Integer = 0
> Dim thumbNewHeight As Integer = 0
>
> If bmpIn.Width > bmpIn.Height Then
> calcRatio = CDec(thumbWidth) / bmpIn.Width
> thumbNewWidth = thumbWidth
> Dim thumbHeightTemp1 As Decimal = bmpIn.Height * calcRatio
> thumbNewHeight = CInt(thumbHeightTemp1)
> Else
> calcRatio = CDec(thumbHeight) / bmpIn.Height
> thumbNewHeight = thumbHeight
> Dim thumbWidthTemp2 As Decimal = bmpIn.Width * calcRatio
> thumbNewWidth = CInt(thumbWidthTemp2)
> End If
>
> bmpOut = New Bitmap(thumbNewWidth, thumbNewHeight)
> Dim g As Graphics = Graphics.FromImage(bmpOut)
> g.InterpolationMode =
> System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic
> g.FillRectangle(Brushes.White, 0, 0, thumbNewWidth,
> thumbNewHeight)
> g.DrawImage(bmpIn, 0, 0, thumbNewWidth, thumbNewHeight)
>
> bmpIn.Dispose()
> Catch
> Return
> End Try
>
> Try
> bmpOut.Save(Response.OutputStream,
> System.Drawing.Imaging.ImageFormat.Jpeg)
> Catch
> Finally
> bmpOut.Dispose()
> End Try
> End Sub
> End Class
>
>
> This seems to work!

 
Reply With Quote
 
James Page
Guest
Posts: n/a
 
      09-26-2008
Bruce -

Thanks for your reply - I'm trying to standardise a particular website to
utilise LINQ and call everything from classes in the app_code folder. I know
some queries are better using SQL but this particular web app has become
unwieldly and because of a major upgrade it seemed like a good idea!! At the
present code is all over the place and many controls were poorly designed -
not to mention increasing my rather poor knowledge of LINQ in the first place.


 
Reply With Quote
 
rstrahl
Guest
Posts: n/a
 
      09-26-2008
The quick answer is that blobs translate into byte[], so if you import your
image field you should end up with a byte[] property on your entity.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
www.west-wind.com/weblog




"James Page" <> wrote in message
news:B243BE27-E880-49B1-A744-...
> Hi all another LINQ question!!
>
> to retrieve and display sql varbinary images I currently use the following
> code:
>
> Imports System.Data.SqlClient
> Imports System.Drawing
> Imports System.Drawing.Imaging
> Imports System.IO
>
>
>
> Partial Class ShowPicture
> Inherits System.Web.UI.Page
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim PictureID As Integer =
> Convert.ToInt32(Request.QueryString("PictureID"))
>
> 'Connect to the database and bring back the image contents & MIME
> type for the specified picture
> Using myConnection As New
> SqlConnection(ConfigurationManager.ConnectionStrin gs("ImageGalleryConnectionString").ConnectionStrin g)
>
> Const SQL As String = "SELECT [MIMEType], [ImageData] FROM
> [Pictures] WHERE [PictureID] = @PictureID"
> Dim myCommand As New SqlCommand(SQL, myConnection)
> myCommand.Parameters.AddWithValue("@PictureID", PictureID)
>
> myConnection.Open()
>
> Dim myReader As SqlDataReader = myCommand.ExecuteReader
> If myReader.Read Then
> Response.ContentType = myReader("MIMEType").ToString()
> Response.BinaryWrite(myReader("ImageData"))
>
>
> End If
> myReader.Close()
> myConnection.Close()
> End Using
> End Sub
>
>
> End Class
>
> I'm now trying to use LINQ to replace the sql elements. Can anyone help me
> convert the above using LINQ?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq or not Linq George ASP .Net 4 11-05-2008 04:53 PM
BLOBS and EJB3 Java and Swing Java 2 08-17-2006 04:08 AM
Reading Blobs from SQL and using it in an Image control of a webfo =?Utf-8?B?SnVuaW9yUHJvZ3JhbW1lcg==?= ASP .Net 9 03-20-2006 12:51 AM
Testing php, mysql, html and blobs Paul HTML 3 11-04-2005 04:31 PM
Blobs and triggers - How to insert information into a Blob field inside a trigger. Itamar Lev Java 0 08-26-2003 07:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57