using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;namespace FCXS.XMZL
{
/// <summary>
/// FrmPhoto 的摘要说明。
/// </summary>
public class FrmPhoto : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pbx;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public FrmPhoto()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmPhoto));
this.panel1 = new System.Windows.Forms.Panel();
this.pbx = new System.Windows.Forms.PictureBox();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
// 
// panel1
// 
this.panel1.Controls.Add(this.pbx);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(672, 408);
this.panel1.TabIndex = 0;
// 
// pbx
// 
this.pbx.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pbx.Dock = System.Windows.Forms.DockStyle.Fill;
this.pbx.Image = ((System.Drawing.Image)(resources.GetObject("pbx.Image")));
this.pbx.Location = new System.Drawing.Point(0, 0);
this.pbx.Name = "pbx";
this.pbx.Size = new System.Drawing.Size(672, 408);
this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pbx.TabIndex = 0;
this.pbx.TabStop = false;
// 
// splitter1
// 
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 408);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(672, 3);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
// 
// panel2
// 
this.panel2.Controls.Add(this.button3);
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.comboBox1);
this.panel2.Controls.Add(this.button1);
this.panel2.Controls.Add(this.label1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 411);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(672, 58);
this.panel2.TabIndex = 2;
// 
// button3
// 
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button3.Location = new System.Drawing.Point(499, 17);
this.button3.Name = "button3";
this.button3.TabIndex = 10;
this.button3.Text = "关闭";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// button2
// 
this.button2.Enabled = false;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Location = new System.Drawing.Point(419, 17);
this.button2.Name = "button2";
this.button2.TabIndex = 9;
this.button2.Text = "保存";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// comboBox1
// 
this.comboBox1.Items.AddRange(new object[] {
   "正常",
   "拉伸",
   "自动",
   "居中"});
this.comboBox1.Location = new System.Drawing.Point(283, 17);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(128, 20);
this.comboBox1.TabIndex = 7;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
// 
// button1
// 
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Location = new System.Drawing.Point(99, 17);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 6;
this.button1.Text = "打开图片文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(203, 25);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 8;
this.label1.Text = "设置显示模式";
// 
// FrmPhoto
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 469);
this.ControlBox = false;
this.Controls.Add(this.panel2);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
this.Name = "FrmPhoto";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "平面图";
this.Load += new System.EventHandler(this.FrmPhoto_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion

解决方案 »

  1.   

    /// <summary>
    /// 打开图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter ="位图文件(*.bmp)|*.bmp|jpg文件(*.jpg)|*.jpg|gif文件(*.gif)|*.gif|所有文件(*.*)|*.*";
    ofd.FilterIndex =0;
    if (ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
    {
    this.pbx.Image = Image.FromFile(ofd.FileName.ToString());
    this.filename = ofd.FileName.ToString();
    ///保存按钮有效
    this.button2.Enabled = true;
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    } private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    try
    {
    switch (this.comboBox1.SelectedIndex)
    {
    case 0:
    this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
    break;
    case 1:
    this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    break;
    case 2:
    this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
    break;
    case 3:
    this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
    break;
    default:
    //this.pbx.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal;
    break;
    }
    }
    catch (Exception Ex)
    {
    MessageBox.Show(Ex.Message);
    }
    // MessageBox.Show(this.comboBox1.SelectedText);
    } /// <summary>
    /// 退出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, System.EventArgs e)
    {
    this.Close();
    } private string filename ="";
    public string getsql ="";
    public string updatesql ="update_xmzl_photo";
    /// <summary>
    /// 数据表名
    /// </summary>
    public string tbname ="xmzl";
    public string bh ="";
    public string bh2 ="";
     
        
    /// <summary>
    /// 加载图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmPhoto_Load(object sender, System.EventArgs e)
    {
    byte[] buffByte = null;
    try
    {
    System.Data.OleDb.OleDbCommand mycmd = new System.Data.OleDb.OleDbCommand(getsql,DB.OleConn());
    if (mycmd.Connection.State != System.Data.ConnectionState.Open)
    {
    mycmd.Connection.Open();
    }
    System.Data.OleDb.OleDbDataReader myrd = mycmd.ExecuteReader();
    while (myrd.Read())
    {
    buffByte = ((byte[])myrd[0]);
    }
    myrd.Close();
    mycmd.Connection.Close();
    //将图像的字节数组放入内存流
    System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
    //通过流对象建立Bitmap
    System.Drawing.Bitmap bmp = new Bitmap(ms);
    this.pbx.Image = bmp;// if (myrd.HasRows)
    // {
    // myrd.Read();
    // if (System.NullReferenceException.Equals(myrd["image"],null))
    // {
    // return;
    // }
    // if (myrd["image"].ToString().Length ==0)
    // {
    // return;
    // }
    // System.IO.MemoryStream mstream = new System.IO.MemoryStream( (byte[])  myrd["image"],0,myrd["image"].ToString().Length);
    // this.pbx.Image = Image.FromStream(mstream);
    // mstream.Close();
    // } }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message+",图片不存在!");
    }            
    } /// <summary>
    /// 保存图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, System.EventArgs e)
    {
    try
    {
    System.IO.FileInfo fs = new System.IO.FileInfo(this.filename);
    if (!fs.Exists)
    {
    MessageBox.Show("无效的文件名!");
    return;
    }
    //创建一字节数组,用来存储图像文件.(数组的长度是图像文件的长度)
    byte[] Content=new byte[fs.Length];
    //打开文件并用他初始化一个文件流对象
    FileStream ImageFileStream=fs.OpenRead();
    //将文件内容写入字节数组
    ImageFileStream.Read(Content,0,Content.Length);
    //关闭文件流
    ImageFileStream.Close(); //创建一个Sql命令对象,用来插入记录
    System.Data.OleDb.OleDbCommand mycmd = new System.Data.OleDb.OleDbCommand(updatesql,DB.OleConn());
    mycmd.CommandType = CommandType.StoredProcedure;
    mycmd.Parameters.Add("@image",System.Data.OleDb.OleDbType.VarBinary);
    mycmd.Parameters.Add("@bh",OleDbType.VarChar,20);
    mycmd.Parameters.Add("@bh2",System.Data.OleDb.OleDbType.VarChar,20);
    mycmd.Parameters.Add("@tbname",System.Data.OleDb.OleDbType.VarChar,50); mycmd.Parameters["@image"].Value = Content;
    mycmd.Parameters["@bh"].Value =bh;
    mycmd.Parameters["@bh2"].Value = bh2;
    mycmd.Parameters["@tbname"].Value = tbname;
    //打开数据库连接
    if (mycmd.Connection.State!=ConnectionState.Open)
    {
    mycmd.Connection.Open();
    }
    //执行 Sql 语句
    mycmd.ExecuteNonQuery();
    //关闭数据库连接
    mycmd.Connection.Close(); MessageBox.Show("图像文件 " + fs.FullName + " 成功上传到数据库!");
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    }  
    }
    }  
      

  2.   

    CREATE proc update_xmzl_photo  
      @image as image,                           ----图片  
      @bh  as varchar(20),                       ---编号(主要)  值  
      @bh2 as varchar(20),                      ---次要编号   值  
      @tbname as varchar(40)                ---要更新的表名  
        
    as  
     if (lower(@tbname) ='xmzl')   
       begin  
          update xmzl set xmzl_photo = @image where xmzl_bh =@bh  
       end  
     if (lower(@tbname)='lfmx')  
        begin  
           update lfmx set lfmx_photo = @image where lfmx_bh =@bh and lfmx_xmbh =@bh2  
        end  
     if (lower(@tbname) ='hxzl')  
        begin  
            update hxzl set hxzl_photo = @image where hxzl_bh = @bh and hxzl_xmbh = @bh2  
        end  
     if (lower(@tbname )='ggxm')  
      begin  
          update ggxm set ggxm_photo = @image where ggxm_bh = @bh   
      end
    GO