LINQ and blobs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmFtZXMgUGFnZQ==?=

    #1

    LINQ and blobs

    Hi all another LINQ question!!

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

    Imports System.Data.Sql Client
    Imports System.Drawing
    Imports System.Drawing. Imaging
    Imports System.IO



    Partial Class ShowPicture
    Inherits System.Web.UI.P age

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim PictureID As Integer =
    Convert.ToInt32 (Request.QueryS tring("PictureI D"))

    'Connect to the database and bring back the image contents & MIME
    type for the specified picture
    Using myConnection As New
    SqlConnection(C onfigurationMan ager.Connection Strings("ImageG alleryConnectio nString").Conne ctionString)

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

    myConnection.Op en()

    Dim myReader As SqlDataReader = myCommand.Execu teReader
    If myReader.Read Then
    Response.Conten tType = myReader("MIMET ype").ToString( )
    Response.Binary Write(myReader( "ImageData" ))


    End If
    myReader.Close( )
    myConnection.Cl ose()
    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?
  • =?Utf-8?B?SmFtZXMgUGFnZQ==?=

    #2
    RE: LINQ and blobs

    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.Sql Client
    Imports System.IO
    Imports System.Drawing
    Imports System.Drawing. Imaging
    Imports System.Data.Lin q


    Partial Class showPicture
    Inherits System.Web.UI.P age


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim imageID As Integer =
    Convert.ToInt32 (Request.QueryS tring("PictureI D"))

    Dim db As New DataClassesData Context()

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

    Dim imgContent As Byte() = query.ToArray()
    Response.Conten tType = ("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(im gContent)


    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 thumbHeightTemp 1 As Decimal = bmpIn.Height * calcRatio
    thumbNewHeight = CInt(thumbHeigh tTemp1)
    Else
    calcRatio = CDec(thumbHeigh t) / bmpIn.Height
    thumbNewHeight = thumbHeight
    Dim thumbWidthTemp2 As Decimal = bmpIn.Width * calcRatio
    thumbNewWidth = CInt(thumbWidth Temp2)
    End If

    bmpOut = New Bitmap(thumbNew Width, thumbNewHeight)
    Dim g As Graphics = Graphics.FromIm age(bmpOut)
    g.Interpolation Mode =
    System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c
    g.FillRectangle (Brushes.White, 0, 0, thumbNewWidth,
    thumbNewHeight)
    g.DrawImage(bmp In, 0, 0, thumbNewWidth, thumbNewHeight)

    bmpIn.Dispose()
    Catch
    Return
    End Try

    Try
    bmpOut.Save(Res ponse.OutputStr eam,
    System.Drawing. Imaging.ImageFo rmat.Jpeg)
    Catch
    Finally
    bmpOut.Dispose( )
    End Try
    End Sub
    End Class


    This seems to work!

    Comment

    • bruce barker

      #3
      Re: LINQ and blobs

      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.Sql Client
      Imports System.IO
      Imports System.Drawing
      Imports System.Drawing. Imaging
      Imports System.Data.Lin q
      >
      >
      Partial Class showPicture
      Inherits System.Web.UI.P age
      >
      >
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles Me.Load
      Dim imageID As Integer =
      Convert.ToInt32 (Request.QueryS tring("PictureI D"))
      >
      Dim db As New DataClassesData Context()
      >
      Dim query As Binary = (From x In db.pictures Where x.ID = imageID
      Select x.image).Single ()
      >
      Dim imgContent As Byte() = query.ToArray()
      Response.Conten tType = ("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(im gContent)
      >
      >
      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 thumbHeightTemp 1 As Decimal = bmpIn.Height * calcRatio
      thumbNewHeight = CInt(thumbHeigh tTemp1)
      Else
      calcRatio = CDec(thumbHeigh t) / bmpIn.Height
      thumbNewHeight = thumbHeight
      Dim thumbWidthTemp2 As Decimal = bmpIn.Width * calcRatio
      thumbNewWidth = CInt(thumbWidth Temp2)
      End If
      >
      bmpOut = New Bitmap(thumbNew Width, thumbNewHeight)
      Dim g As Graphics = Graphics.FromIm age(bmpOut)
      g.Interpolation Mode =
      System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c
      g.FillRectangle (Brushes.White, 0, 0, thumbNewWidth,
      thumbNewHeight)
      g.DrawImage(bmp In, 0, 0, thumbNewWidth, thumbNewHeight)
      >
      bmpIn.Dispose()
      Catch
      Return
      End Try
      >
      Try
      bmpOut.Save(Res ponse.OutputStr eam,
      System.Drawing. Imaging.ImageFo rmat.Jpeg)
      Catch
      Finally
      bmpOut.Dispose( )
      End Try
      End Sub
      End Class
      >
      >
      This seems to work!

      Comment

      • =?Utf-8?B?SmFtZXMgUGFnZQ==?=

        #4
        Re: LINQ and blobs

        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.


        Comment

        • rstrahl

          #5
          Re: LINQ and blobs

          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





          "James Page" <JamesPage@disc ussions.microso ft.comwrote in message
          news:B243BE27-E880-49B1-A744-680097F57B80@mi crosoft.com...
          Hi all another LINQ question!!
          >
          to retrieve and display sql varbinary images I currently use the following
          code:
          >
          Imports System.Data.Sql Client
          Imports System.Drawing
          Imports System.Drawing. Imaging
          Imports System.IO
          >
          >
          >
          Partial Class ShowPicture
          Inherits System.Web.UI.P age
          >
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As
          System.EventArg s) Handles Me.Load
          Dim PictureID As Integer =
          Convert.ToInt32 (Request.QueryS tring("PictureI D"))
          >
          'Connect to the database and bring back the image contents & MIME
          type for the specified picture
          Using myConnection As New
          SqlConnection(C onfigurationMan ager.Connection Strings("ImageG alleryConnectio nString").Conne ctionString)
          >
          Const SQL As String = "SELECT [MIMEType], [ImageData] FROM
          [Pictures] WHERE [PictureID] = @PictureID"
          Dim myCommand As New SqlCommand(SQL, myConnection)
          myCommand.Param eters.AddWithVa lue("@PictureID ", PictureID)
          >
          myConnection.Op en()
          >
          Dim myReader As SqlDataReader = myCommand.Execu teReader
          If myReader.Read Then
          Response.Conten tType = myReader("MIMET ype").ToString( )
          Response.Binary Write(myReader( "ImageData" ))
          >
          >
          End If
          myReader.Close( )
          myConnection.Cl ose()
          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?

          Comment

          Working...