MSDN上有一点Clue,不确定能不能实现,试一下吧!:)
(假设Graphic字段包含的是图片的全路经文件名,其它应该类同)Image类型不支持DataBinding(它在Drawing下面),要用PictureBox才行,而PictureBox需要一个Image作为属性,而不是文件路径名。
Binding类有两个事件Format和Parse,是用于定制用户显示格式的:PicutreBox mypic = new PictureBox();
... //设置其他显示属性, 生成DataSet ds。
Binding picbd = new Binding("Image", ds, "MyRecords.Photo");
picbd.Format += new ConvertEventHandler(PathToImage);
picbd.Parse += new ConvertEventHandler(ImageToPath);
mypic.DataBindings.Add(picbd);
...private void PathToImage(object o, ConvertEventArgs arg) {
if (arg.DesiredType != TypeOf(Image)) return;
arg.Value = new Bitmap((string)arg.Value);
}private void ImageToPath(object o, ConvertEventArgs arg) {
if (arg.DesiredType != TypeOf(string)) return;
arg.Value = strFilePath; //you need to save the path name somewhere, cause you cannot get it from a Bitmap object.
}
...
(假设Graphic字段包含的是图片的全路经文件名,其它应该类同)Image类型不支持DataBinding(它在Drawing下面),要用PictureBox才行,而PictureBox需要一个Image作为属性,而不是文件路径名。
Binding类有两个事件Format和Parse,是用于定制用户显示格式的:PicutreBox mypic = new PictureBox();
... //设置其他显示属性, 生成DataSet ds。
Binding picbd = new Binding("Image", ds, "MyRecords.Photo");
picbd.Format += new ConvertEventHandler(PathToImage);
picbd.Parse += new ConvertEventHandler(ImageToPath);
mypic.DataBindings.Add(picbd);
...private void PathToImage(object o, ConvertEventArgs arg) {
if (arg.DesiredType != TypeOf(Image)) return;
arg.Value = new Bitmap((string)arg.Value);
}private void ImageToPath(object o, ConvertEventArgs arg) {
if (arg.DesiredType != TypeOf(string)) return;
arg.Value = strFilePath; //you need to save the path name somewhere, cause you cannot get it from a Bitmap object.
}
...
解决方案 »
- 如何在C# winform中实现 实时扫描SQL SERVER 2005中某张表有数据更新?
- 如何将一个WORD文档中指定颜色的字符保存到另一个WORD文档中
- 求教!关于类型化DataSet
- 如何获取MP3播放长度
- Struct 和class的区别?
- 在开发一个视频网站遇到的数据库问题大家看看
- C# 如何将一系列地理坐标画到picturebox中,显示呢?
- c#如何绕过打印机驱动程序向打印机端口发字符串进行打印?内有VB的代码,谁翻译一下
- 一个登录程序,遇到一些问题
- 如何做到当拖动窗体中的一个控件时,窗体跟着动?
- 已经有一个项目做好了
- vb中的IsNumberic在c#中是什么?在线等待。。。
如果是web form:imagebox的src属性指向一个binarywrite(field)的aspx
记得在C:下放上1.jpg, 2.jpg, 3.jpg.using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;namespace MChen.LearnCSharp.MyForm {
internal class MyDB {
public static DataSet GetDSet() {
DataSet ds = new DataSet("myDataSet");
DataTable tCust = new DataTable("Info");
DataColumn cCustID = new DataColumn("ImagePath", typeof(string));
tCust.Columns.Add(cCustID);
ds.Tables.Add(tCust);
DataRow newRow1;
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
tCust.Rows.Add(newRow1);
}
tCust.Rows[0]["ImagePath"] = @"c:\1.jpg";
tCust.Rows[1]["ImagePath"] = @"c:\2.jpg";
tCust.Rows[2]["ImagePath"] = @"c:\3.jpg";
return ds;
}
}
public class MyForm: Form {
private PictureBox _pic;
private Button _btnnext, _btnprev;
private string _str = @"c\1.jpg";
private DataSet _ds;
private BindingManagerBase _img;
private void PathToImage(object o, ConvertEventArgs arg) {
if (arg.DesiredType != typeof(Image)) return;
_str = (string)arg.Value;
arg.Value = new Bitmap((string)arg.Value);
} private void ImageToPath(object o, ConvertEventArgs arg) {
if (arg.DesiredType != typeof(string)) return;
arg.Value = _str;
}
public MyForm ()
{
Text = "hello";
Width = 360; Height = 200;
_pic = new PictureBox();
_pic.Width = Width - 100;
_pic.Height = Height;
_pic.Image = new Bitmap(@"c:\test.jpg");
_pic.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add( _pic );
_ds = MyDB.GetDSet();
Binding picbd = new Binding("Image", _ds, "Info.ImagePath");
picbd.Format += new ConvertEventHandler(PathToImage);
picbd.Parse += new ConvertEventHandler(ImageToPath);
_pic.DataBindings.Add(picbd);
_btnnext = new Button();
_btnnext.Text = "Next";
_btnnext.Left = Width - 100;
_btnnext.Width = 100;
_btnnext.Top = 0;
_btnnext.Height = 100;
_btnnext.Click += new EventHandler(OnNextClick);
Controls.Add(_btnnext);
_btnprev = new Button();
_btnprev.Text = "Prev";
_btnprev.Left = Width - 100;
_btnprev.Width = 100;
_btnprev.Top = 100;
_btnprev.Height = 100;
_btnprev.Click += new EventHandler(OnPrevClick);
Controls.Add(_btnprev);
ClientSize = new Size(Width, Height);
_img = BindingContext[_ds, "Info"];
}
public void OnNextClick(object o, EventArgs arg) {
Console.WriteLine(_img.Position);
_img.Position += 1;
}
public void OnPrevClick(object o, EventArgs arg) {
Console.WriteLine(_img.Position);
_img.Position -= 1;
}
public static void Main()
{
Application.Run(new MyForm());
}
}
}