我使用此代码(Image
)将byte[]
转换为Form.cs
:
private void button1_Click(object sender, EventArgs e)
{
Form1 fom = new Form1();
ImageConverter converter = new ImageConverter();
byte[] imgArray = (byte[])converter.ConvertTo(fom.panel1.BackgroundImage, typeof(byte[]));
ImageData img = new ImageData
{
ClassName = textBox1.Text,
Password = textBox2.Text,
Image = imgArray,
};
using (BoardDatabaseEntities dc = new BoardDatabaseEntities())
{
dc.ImageDatas.Add(img);
dc.SaveChanges();
MessageBox.Show("Saved into database");
}
this.Close();
}
然后,我试图将其转换回Image
::
protected void Button1_Click(object sender, EventArgs e)
{
using (BoardDatabaseEntities dc = new BoardDatabaseEntities())
{
var v = dc.ImageDatas.Where(a => a.ClassName.Equals(TextBox3.Text) && a.Password.Equals(TextBox4.Text)).FirstOrDefault();
if (v != null)
{
byte[] byt = v.Image;
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(byt);
String name = v.ClassName;
try
{
var ratio = (double)100 / img.Height;
int imageHeight = (int)(img.Height * ratio);
int imageWidth = (int)(img.Width * ratio);
System.Drawing.Image.GetThumbnailImageAbort dCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbnailImg = img.GetThumbnailImage(imageWidth, imageHeight, dCallback, IntPtr.Zero);
thumbnailImg.Save(Path.Combine(Server.MapPath("~/Thumbnail"), name), ImageFormat.Jpeg);
thumbnailImg.Dispose();
//Here Code for Get Uploaded Images
PopulateImage();
}
catch (Exception ex)
{
throw;
}
}
}
}
它给我一个ArgumentException
错误:
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(byt);
它说,参数无效。