Beth's Chinese blog
In SQL-Server we can store images inside database tables directly using the Image column type. And with .NET 2.0 data binding it can automatically convert these images (stored as byte arrays) into System.Drawing.Image classes for you. For instance, say you have a table called Pictures that has a column called Picture of the data type Image. You can create a new data source to this table to generate a strongly typed DataSet and use drag-and drop data binding to automatically display the image in a PictureBox. (For information on connecting to your database and creating strongly typed DataSets watch this video.)
If you don't see a PictureBox icon in the Data Sources window next to the Picture column in your DataTable, you may need to select the PictureBox to associate the byte array with the control:
Now you can drag-and-drop the Picture onto your form to set up the data binding to the PictureBox. The binding will automatically handle converting the byte array stored in the column into an image using the System.Drawing.ImageConverter. You can then set the SizeMode property on the PictureBox depending on how you want the image displayed; resized, stretched or otherwise.
But what if you just want to resize the image and not use a PictureBox? In that case you just need to convert the byte array stored in the Picture column into an System.Drawing.Image. Once you have that then you can use methods on this Image class to perform all sorts of transformations. To resize the image, just call GetThumbnailImage and pass it the new size requirements:
Dim resizeImg, origImg As Image
'Get the current row
Dim row As PictureDemoDataSet.PictureRow
row = CType(CType(Me.PictureBindingSource.Current, DataRowView).Row, _
PictureDemoDataSet.PictureRow)
'Convert byte array to image
Using ms As New System.IO.MemoryStream(row.Picture)
origImg = Image.FromStream(ms)
Dim width As Integer = 25
Dim height As Integer = 25
'Resize image
resizeImg = origImg.GetThumbnailImage(width, height, Nothing, Nothing)
End Using
I've attached a simple application that demonstrates these techniques for you to play with. You'll need Visual Basic 2005 and SQL Server 2005 (or Express) to run.
Enjoy!
I just posted another great interview on Channel 9 with Young Joo talking about LINQ to SQL and the new O/R Designer in Visual Studio 2008. He demos a typical business client-server scenario and shows how LINQ to SQL classes make it much easier to work with relational data in SQL Server 2005. Young also talks about architectures where he sees using LINQ to SQL having the most benefits.
Learn more about the O\R Designer and LINQ to SQL by viewing our "How Do I" video LINQ series on the Visual Basic Developer Center.
So you have an application you've written in Visual Studio 2005 and you want to dig in, convert this baby, and start using LINQ in Visual Studio 2008. In this post I'm going to outline what steps are involved to get LINQ working based on what providers you may want to use.
In Visual Studio 2008 there's a new multi-targeting feature that allows you to work in Visual Studio 2008 but be able to write applications targeting either the .NET 2.0, 3.0 or 3.5 Frameworks. ScottGu has a good post that talks about this and so does Rick Strahl. This means that going forward you won't have to load multiple versions of the Visual Studio IDE onto your machine in order to work on applications targeting previous versions of the framework. (Note: If you are targeting .NET 1.0 or 1.1 frameworks then you'll still have to load 2002 or 2003 IDEs.) This is good news because it not only saves disk space and context switching, it allows you to take advantage of the new IDE tools like debuggers and editors without the risk of upgrading your current projects. However when you do want to upgrade a project to 3.5 to start taking advantage of LINQ, you need to import some of the new namespaces yourself.
When you first open up a project written in Visual Studio 2005 inside Visual Studio 2008 it will prompt you to upgrade the project. What this actually does is update the project (.vbproj) and solution .(sln) files to be compatible with VS 2008. The project file is actually still backwards compatible with VS 2005 so you can still open it there, the solution file however, is just one way. So if you have a team of developers working with a mix of both VS 2005 and VS 2008 IDEs you'll need to keep two solution files, but luckily your project files (which change much more often than solution configurations) can be shared.
Now all this upgrade process does is upgrade your project and solution files, your application will still be targeting the .NET 2.0 Framework. In order to upgrade your apps to use new features like LINQ you'll need to change the target framework and add some references. You'll also want to turn on the new Option Infer feature. This allows the compiler to infer local variable type declarations by evaluating the right-hand side of the expression. This becomes extremely useful when writing LINQ queries. To enable this, double-click on My Project in the Solution Explorer to open the project properties and select the Compile tab. Under Option Infer select "On".
Now to change the target framework, click the "Advanced Compile Options..." button and you can change the target in the dropdown:
Select the 3.5 Framework, click OK, and the project will be closed and reopened. If you open the project properties again and look on the References tab you'll notice that the System.Core.dll version 3.5 is now being referenced automatically for you. However, in order to start using LINQ you'll need to import a couple namespaces and add a couple more references depending on which LINQ providers you want to use. To enable pure LINQ to Objects, on the References tab under Imported Namespaces select System.Linq. You'll now be able to write queries that work over objects like:
Dim currentFiles = From File In My.Computer.FileSystem.GetFiles(CurDir) _
Select My.Computer.FileSystem.GetFileInfo(File)
In order to write queries that work over DataSets you'll need to add a reference to System.Data.DataSetExtensions then you'll need to re-run the Dataset generator on the DataSets you want to write LINQ queries over. Just right-click on the DataSet and select "Run Custom Tool". This will regenerate the DataSet code so that the DataTables now inherit from a LINQ-ready class called TypedTableBase which is in the System.Data.DataSetExtension namespace. You'll now be able to write queries that work over typed DataSets. For instance, you can now write:
Dim total = Aggregate Products In Me.CategoryProductDataSet.Products _
Where Products.CategoryID = 1 AndAlso _
Products.Discontinued = False _
Into Sum(Products.UnitPrice * Products.UnitsInStock)
If you want to start using the LINQ to XML provider you'll need to add a reference to System.Xml.Linq.dll and import the namespace System.Xml.Linq. You'll now be able to write queries that work over XML like:
Dim survey = XElement.Load(CurDir() & "\questions.xml")
Dim questions = From q In survey...<question> Select q
Finally, if you want to start using LINQ to SQL classes in your newly upgraded project, it's really easy. Just right-click and select Add New Item and select the LINQ to SQL classes template which opens up the new O/R designer and automatically adds the reference to System.Data.Linq.dll for you. This allows you to write queries like this (against SQL-server):
Dim countryList = From Customer In Db.Customers _
Where Customer.Country <> "" _
Order By Customer.Country _
Select Customer.Country Distinct
So I hope that clears up how to get started with LINQ in your current applications that you're bringing into VS 2008. So what are you waiting for? ;-) Happy LINQing.
Bill Burrows has done it again and created a series of videos to help get you started with LINQ to SQL in Visual Basic! Bill is a great teacher and the style of these videos is off-the-cuff so that you really feel like you are in a classroom learning real technical content and you're not missing anything. It takes a special kind of person to do this kind of video because personality really shines through. Bill's got a great personality and I'm thankful for the time he's spent on these videos for the Visual Basic Community. I hope you get as much out of these lessons as I did!
I just released three more LINQ how-to videos, #3, #4 and #5 and they're up on the Developer Center now. One is an example of how to use LINQ to DataSets and the other two focus on LINQ to SQL and the new O\R designer.
If you're thinking about converting your Visual Studio 2005 apps to Visual Studio 2008 and are using DataSets right now, you'll want to take a look at the LINQ over DataSets video. You don't have to re-architect your applications in order to take advantage of Language Integrated Query right now. There is a built-in LINQ over DataSet provider that allows you to write queries right over your current DataSets.
But why stop there :-) ?
LINQ to SQL is a built-in infrastructure that allows you to create object models directly from tables in your SQL databases and use them in your Visual Basic applications. These objects can be custom business objects that you extend with partial methods or even inherit from your own base class. And best of all, LINQ to SQL manages changes in these objects for you automatically, and it can be as easy as using a DataSet. If you're creating new data-based applications or looking at upgrading and extending your current ones, this is a technology that you won't want to miss. So get on learning it now by watching these videos.
Also please feel free to rate the videos and send me feedback by clicking on the stars in the right-hand side of the landing pages (on a scale of 1 to 5 stars, 5 being the best). I actually do read all of them and so do our site managers!
I just posted an interview with Amanda on Channel 9 where she walks us through the new LINQ syntax available in VS 2008 Beta 2. I also posted some How Do I videos on LINQ to help get you started -- look for more of these beginning of next week!
In this interview, Amanda Silver, a Lead Program Manager on the Visual Basic Team, demonstrates new LINQ syntax around Joins and Aggregates that is now available in Visual Studio 2008 Beta 2. Amanda is a guru of LINQ in Visual Basic and gives insightful explanations of how to use this new syntax.
The interview starts out a little funny because the first 20 seconds of my video tape got chewed up so we had to reshoot the intro later in the day. Amanda thought of a funny way to make the transition as you'll see. See if you can pinpoint the movie that inspired her!
I just posted an interview with Lisa on Channel9 that you'll probably want to check out -- showing the new and much much imporoved Intellisense for Visual Basic in Visual Studio 2008:
In this interview Lisa Feigenbaum, a Program Manager on the Visual Basic Editor, shows us all the improvements made to the Intellisense support in Visual Basic including keyword and local variable Intellisense, new language feature support, and top customer requests. Although every feature is important, the biggest chunk of the work this cycle was supporting the new language features including LINQ. As Lisa puts it, "The most time was actually spent on the language features because the compiler team went crazy this release" [laughter ensues]. Join Lisa and I as she demonstrates exactly what we can look forward to with Visual Basic Intellisense in Visual Studio 2008!
Kerby just posted Rob Windsor's first article onto the community page on the Visual Basic Developer Center. In this article Rob walks us through creating our first WCF service, hosts and client. If you're just getting started with Windows Communication Foundation in .NET 3.0, this is a great place to start.
Rob is a Visual Basic MVP and is with ObjectSharp Consulting up in Toronto, Canada. Many thanks to Rob for submitting an article to the VB community! He's now made it into the featured MVP section of the community page and the home page of the Dev Center, along with Jim Duffy who submitted the first article to kick off the new content section. Nice mug shots, guys!
If you're interested in submitting an article for the Visual Basic Developer Center please contact me.
I just released two videos and started a new series on LINQ in Visual Basic. I'm really excited about these new language features in this new version. I'll be adding more in the weeks to come so check back often!
Also, if you love the video style of learning then you'll definitely want to check out more that are available on MSDN:
The VB Team just released an updated version of the Visual Basic Power Packs on the VB Developer Center that includes a new set of controls for Visual Studio 2005 that allow you to draw lines, rectangle and oval shapes on your Windows Forms. These controls encapsulate many of the graphics methods that are contained in the System.Drawing namespace. This enables you to draw lines and shapes in a single step without having to create graphics objects, pens, and brushes. Complex graphics techniques such as gradient fills can be accomplished by just setting some properties.
I don't know about you but I've been really looking forward to this release!