473,769 Members | 7,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Sep 25 '08 #1
4 1395
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!
Sep 26 '08 #2
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!
Sep 26 '08 #3
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.
Sep 26 '08 #4
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" <Ja*******@disc ussions.microso ft.comwrote in message
news:B2******** *************** ***********@mic rosoft.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?
Sep 26 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3362
by: Kirby Urner | last post by:
I've been testing the Cookbook example 8.6 (2002 edition) re using cPickle to insert and retrieve BLOBs from mySQL, using Python's MySQLdb module. When I try to cPickle.loads(blob), I get an error telling me that loads wants a string, not type array.array. So I go cPickle.loads(blob.tostring()) instead and it works. My question is: has something changed in the Python API since this example was written?
0
2888
by: Ole Hansen | last post by:
Hi, I have a working application inserting rows to a table using the array interface. Now I want to insert BLOBs as well but OCIStmtExecute crashes when executed with BLOBs. For BLOB types I use OCIDescriptorAlloc() and uses this to allocate space for my data (app. 1Mb). Afterwards I store data in memory allocated by OCIDescripterAlloc.
0
2889
by: Ole Hansen | last post by:
Hi, Is it at all possible to insert BLOBs using the Array Interface? Today I have an application using the array interface. It works fine but so far I haven't been using BLOBs. I insert 100-200 rows in one server round trip. Now I want to have one or more colums of type BLOB but I cant see how this can fit into my current application.
1
1699
by: picaza | last post by:
hi, does anyone have a perl example of loading blobs from either a bin file or an input stream? much thanks, peter
2
1900
by: Ike | last post by:
I have blob fields in some tables - never more than one field in any table, and never where a blob can be > 64k. I have inherited the code for this project from someone else, and was considering investing considerable effort in this case to move the blobs over to a separate table altogether. I am wondering if it is worth it however. Is it disasterous to have a single blob field (which frequently is merely null) in a table. I have three...
7
6950
by: Howard Lowndes | last post by:
My situation is that I am interacting PHP 4.1.2 to PostgreSQL 7.2.2 I have no difficulty inserting and managing BLOBs into the Large Object system table, and I have a user table called images which maintains the relationship between the BLOB loid and the identity that relates to it in my user tables. So far so good. When I RTFM obout psql it refers to the \lo_import, \lo_list, \lo_export and \lo_unlink functions.
7
4054
by: Nilabhra Banerjee | last post by:
Hi, I am still not sure whether the BLOBS are actually stored in the database or they have the pointer to the database for that file in the filesystem. If I remove the files (sources) for BLOBS from the directories with the BLOB still hold the data ? Also one more very intriguing part is that if BLOBS are not deleted if we delete them from tables how to
0
1468
by: Bing | last post by:
Hi there, I am using the DB2 universal JDBC driver type 4 to insert BLOBs into a DB2 database. The Method I used for supplying the BLOB data value is setBinaryStream(). Everything works fine as long as the binary data is less than or equal to 100k. Anything larger than that (even for one byte) will result an exception saying the value is too large. All the DB2 samples (at least two)I have found have a limit of 100k (magic number!!!),...
2
2820
by: Jerry LeVan | last post by:
Hi, I am just getting into large objects and bytea "stuff". I created a small db called pictures and loaded some large objects and then tried to do a restore. Here is how I got the dump. pg_dump -Fc -b pictures > /Users/jerry/desktop/db.comp
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.