1.xaml
<Window x:Class="WpfUploadDispalyIMG.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="800">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="14,12,0,0" Name="FileNameTextBox" VerticalAlignment="Top" Width="394" IsEnabled="False" />
<Button Content="瀏覽" Height="23" HorizontalAlignment="Left" Margin="414,12,0,0" Name="btBrowse" VerticalAlignment="Top" Width="49" Click="btBrowse_Click" />
<Button Content="上傳" Height="23" HorizontalAlignment="Left" Margin="469,12,0,0" Name="btUpdate" VerticalAlignment="Top" Width="49" Click="btUpdate_Click" />
<Image Margin="12,41,0,0" Name="image1" Stretch="Fill" Width="200" Height="200" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="590,13,0,0" Name="tbName" VerticalAlignment="Top" Width="46" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="529,16,0,0" Name="textBlock1" Text="輸入姓名:" VerticalAlignment="Top" />
<Button Content="查看照片" Height="23" HorizontalAlignment="Left" Margin="642,13,0,0" Name="btShow" VerticalAlignment="Top" Width="65" Click="btShow_Click" />
<Button Content="保存" Height="23" HorizontalAlignment="Left" Margin="708,13,0,0" Name="btSave" VerticalAlignment="Top" Width="65" Click="btSave_Click" />
</Grid>
</Window>
2.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;
using System.Data.Linq;
namespace WpfUploadDispalyIMG
{
public partial class MainWindow : Window
{
DataClasses1DataContext db = new DataClasses1DataContext();
public MainWindow()
{
InitializeComponent();
}
//添加圖片並瀏覽
private void btBrowse_Click(object sender, RoutedEventArgs e)
{
//創建"打開文件"對話框
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//設置文件類型過濾
dlg.Filter = "圖片|*.jpg;*.png;*.gif;*.bmp;*.jpeg";
// 調用ShowDialog方法顯示"打開文件"對話框
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//獲取所選文件名並在FileNameTextBox中顯示完整路徑
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
//在image1中預覽所選圖片
BitmapImage image = new BitmapImage(new Uri(filename));
image1.Source = image;
image1.Width = image.Width;
image1.Height = image.Height;
}
}
//上傳圖片至數據庫
private void btUpdate_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(FileNameTextBox.Text))
{
UploadIMG();
MessageBox.Show("OK!");
FileNameTextBox.Text = string.Empty;
}
else
MessageBox.Show("請選擇圖片!");
}
private void UploadIMG()
{
//將所選文件的讀入字節數組img
byte[] img = File.ReadAllBytes(FileNameTextBox.Text);
string fileName = System.IO.Path.GetFileNameWithoutExtension(FileNameTextBox.Text);
//FileNameTextBox.Text.Substring(FileNameTextBox.Text.LastIndexOf('\\')+1);
Crew newCrew = new Crew()
{
//[key]=[value]
photo = img,//將圖片寫入數據庫
name = fileName
};
db.Crew.InsertOnSubmit(newCrew);
db.SubmitChanges();
}
//顯示圖片
private void btShow_Click(object sender, RoutedEventArgs e)
{
try
{
//根據文件名讀取二進制形式的圖片
Binary p = db.Crew.FirstOrDefault(c => c.name == tbName.Text.Trim()).photo;
byte[] img = p.ToArray();
ShowSelectedIMG(img);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ShowSelectedIMG(byte[] img)
{
BitmapImage image = new BitmapImage();
//以流的形式初始化圖片
image.BeginInit();
image.StreamSource = new MemoryStream(img);
image.EndInit();
image1.Source = image;
image1.Width = image.PixelWidth;
image1.Height = image.PixelHeight;
}
//保存爲文件
private void btSave_Click(object sender, RoutedEventArgs e)
{
//創建"保存文件"對話框
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
//設置默認文件類型
dlg.DefaultExt = ".png";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//獲取要保存文件名的完整路徑
string filename = dlg.FileName;
//將文件流寫入文件
FileStream write = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, System.IO.FileShare.Read);
Binary p = db.Crew.FirstOrDefault(c => c.name == tbName.Text.Trim()).photo;
byte[] img = p.ToArray();
write.Write(img, 0, img.Count());
write.Close();
MessageBox.Show("成功保存");
}
}
}
}
文章轉自:http://www.cnblogs.com/Laro/archive/2011/05/23/2054009.html