首页 新闻 搜索 专区 学院

Bitmap bmp = new Bitmap(new MemoryStream(data)) 函数无效

0
悬赏园豆:10 [已关闭问题]

 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();
            }
        }

励箭的主页 励箭 | 初学一级 | 园豆:47
提问于:2010-07-19 17:27
< >
分享
其他回答(1)
0

传进的参数 data无效吧

winzheng | 园豆:8797 (大侠五级) | 2010-07-19 21:14
winzheng 大侠五级 请问怎么修改呢?我是按照hello silverlight4 书中的代码写的
支持(0) 反对(0) 励箭 | 园豆:47 (初学一级) | 2010-07-20 08:38
0

sssssssss

kerry_tz | 园豆:202 (菜鸟二级) | 2013-06-08 13:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册