public void UploadPhotoData(byte[] data)
{
Guid guid = Guid.NewGuid();
string guidString = guid.ToString();
string photoPath = "/photos/" + guidString + ".jpg";
string thumbPath = "/photos/" + guidString + "_thumb.jpg";
Bitmap bmp = new Bitmap(new MemoryStream(data),true);
bmp.Save(HostingEnvironment.MapPath(photoPath), ImageFormat.Jpeg);
Size sz = bmp.Size;
double scale = sz.Width / 100d;
if (sz.Height > sz.Width)
{
scale = sz.Height / 100d;
}
sz.Width = (int)(sz.Width / scale);
sz.Height = (int)(sz.Height / scale);
Bitmap bmpThumb = new Bitmap(bmp, sz);
bmpThumb.Save(HostingEnvironment.MapPath(thumbPath), ImageFormat.Jpeg);
Photo p = new Photo();
p.Url = photoPath;
p.ThumbUrl = thumbPath;
InsertPhoto(p);
this.Submit(this.ChangeSet);
}
调用代码:
private void btnUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image Files (*.jpg, *.jpeg, *.png)|*.jpg;*.jpeg;*.png";
bool? ret = dlg.ShowDialog();
if (ret != null && ret.Value == true)
{
FileInfo file = dlg.File;
using (Stream fileStream = file.OpenRead())
{
UploadPhoto(fileStream);
}
}
}
void UploadPhoto(Stream stream)
{
PhotoContext context = photoDomainDataSource.DomainContext as PhotoContext;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
InvokeOperation response = context.UploadPhotoData(data);
response.Completed += new EventHandler(response_Completed);
}
void response_Completed(object sender, EventArgs e)
{
if (photoDomainDataSource.IsBusy == false)
{
photoDomainDataSource.Load();
}
}
传进的参数 data无效吧
sssssssss