我只是想构建一个小型的 C# .Net 4.0 应用程序,使用Windows Forms(WPF 我不了解,至少有点了解 Windows Forms :-) )。
是否可以直接将 System.Drawing.Bitmap
对象绑定到 PictureBox
的 Image
属性?我尝试使用 PictureBox.DataBindings.Add(...)
,但似乎不起作用。
我应该怎么做呢?
谢谢并致以最良好的祝愿,
Oliver
我只是想构建一个小型的 C# .Net 4.0 应用程序,使用Windows Forms(WPF 我不了解,至少有点了解 Windows Forms :-) )。
是否可以直接将 System.Drawing.Bitmap
对象绑定到 PictureBox
的 Image
属性?我尝试使用 PictureBox.DataBindings.Add(...)
,但似乎不起作用。
我应该怎么做呢?
谢谢并致以最良好的祝愿,
Oliver
Bitmap bitmapFromFile = new Bitmap("C:\\temp\\test.bmp");
pictureBox1.Image = bitmapFromFile;
pictureBox1.Image = new Bitmap("C:\\temp\\test.bmp");
this.PictureBox.DataBindings.Add(new Binding("Visible", this.bindingSource1, "HasPhoto", false, DataSourceUpdateMode.OnPropertyChanged));
this.PictureBox.DataBindings.Add(new Binding("Image", this.bindingSource1, "MyPhoto",false, DataSourceUpdateMode.OnPropertyChanged));
[NotMapped]
public System.Drawing.Image MyPhoto
{
get
{
if (Photo == null)
{
return BlankImage;
}
else
{
if (Photo.Length == 0)
{
return BlankImage;
}
else
{
return byteArrayToImage(Photo);
}
}
}
set
{
if (value == null)
{
Photo = null;
}
else
{
if (value.Height == BlankImage.Height) // cheating
{
Photo = null;
}
else
{
Photo = imageToByteArray(value);
}
}
}
}
[NotMapped]
public Image BlankImage {
get
{
return new Bitmap(1,1);
}
}
public static byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, ImageFormat.Gif);
return ms.ToArray();
}
public static Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
您可以做:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("yourfile.bmp");
picturebox1.Image = bmp;