Thursday, August 27, 2009

Image loading and saving in database in WPF using VB :-

WPF image uploading :-

In many forms we need to upload an image..In WPF we have imagebox in which we can upload an image..For that code is :-

<Button Height="23" HorizontalAlignment="Right" Margin="0,100,35,0" Name="btnBrowse" VerticalAlignment="Top" Width="75">Browse</Button>

Above is the code for button which will be used for browsing file path of that image.

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnBrowse.Click
Dim ofd As New OpenFileDialog
If ofd.ShowDialog() Then
Dim fs As New FileStream(ofd.FileName, FileMode.Open)
Dim imgs As New ImageSourceConverter
Dim dat(fs.Length) As Byte
fs.Read(dat, 0, fs.Length)
fs.Close()
imgcompImage.SetValue(Image.SourceProperty, imgs.ConvertFromString(ofd.FileName.ToString()))
End If
End Sub

This code is the code which uploads image and saves it in database..For using few functions like ShowDialog() etc we need to import few namespaces as given below :-

Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports Microsoft.Win32
Imports System.Windows.Controls.Image
Imports System.Windows.Media.Imaging
Imports System.IO.MemoryStream

OpenFileDialog is to open a file while ShowDialog shows us the file path..SetValue function shows us the image on our page..To store image in database we read image in bytes as shown above in code and then store it in database..So now you can use image uploader in ur page..

1 comment: