LINQ and blobs

Discussion in 'ASP .Net' started by James Page, Sep 25, 2008.

  1. James Page

    James Page Guest

    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?
     
    James Page, Sep 25, 2008
    #1
    1. Advertising

  2. James Page

    James Page Guest

    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!
     
    James Page, Sep 26, 2008
    #2
    1. Advertising

  3. James Page

    bruce barker Guest

    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.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!
     
    bruce barker, Sep 26, 2008
    #3
  4. James Page

    James Page Guest

    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.
     
    James Page, Sep 26, 2008
    #4
  5. James Page

    rstrahl Guest

    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:...
    > 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?
     
    rstrahl, Sep 26, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVuaW9yUHJvZ3JhbW1lcg==?=

    Reading Blobs from SQL and using it in an Image control of a webfo

    =?Utf-8?B?SnVuaW9yUHJvZ3JhbW1lcg==?=, Mar 19, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    467
    =?Utf-8?B?SnVuaW9yUHJvZ3JhbW1lcg==?=
    Mar 20, 2006
  2. Itamar Lev
    Replies:
    0
    Views:
    1,849
    Itamar Lev
    Aug 26, 2003
  3. Paul
    Replies:
    3
    Views:
    442
  4. Java and Swing

    BLOBS and EJB3

    Java and Swing, May 24, 2006, in forum: Java
    Replies:
    2
    Views:
    699
  5. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    362
    Mike Gleason jr Couturier
    Nov 5, 2008
Loading...

Share This Page