推荐文章

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste

WPF上传/显示/下载图片文件(保存在数据库)

日期:2018-07-04 点击:1637 来源:PB2.CN

WPF上传/显示/下载图片文件(保存在数据库)

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()
            {
                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("成功保存");
            }
        }
    }
}


这篇文档对您是否有帮助?

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste