LINQ and blobs

J

James Page

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.ConnectionStrings("ImageGalleryConnectionString").ConnectionString)

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?
 
J

James Page

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.HighQualityBicubic
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!
 
B

bruce barker

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

James Page

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.
 
R

rstrahl

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

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top