从流中更改图像大小

时间:2015-05-01 13:48:25

标签: c# azure model-view-controller

我在这里搜索过这方面的帮助,但没有什么能比得上我所需要的。我有一个上传的图片,我想在保存到azure之前更改大小。

所以目前我的代码是:

public ActionResult UserDetails(HttpPostedFileBase photo)
    {     var inputFile = new Photo()
            {
                FileName = photo.FileName,                    
                Data = () => photo.InputStream
            };
//then I save to Azure

我如何将photo.InputStream更改为100x 100像素?

1 个答案:

答案 0 :(得分:7)

我是这样做的:

byte[] imageBytes; 

//Of course image bytes is set to the bytearray of your image      

using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        using (Image img = Image.FromStream(ms))
        {
            int h = 100;
            int w = 100;

            using (Bitmap b = new Bitmap(img, new Size(w,h)))
            {
                using (MemoryStream ms2 = new MemoryStream())
                {
                    b.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageBytes = ms2.ToArray();
                }
            }
        }                        
    }    

从那里,我使用MemoryStream上传。我使用blob存储并使用UploadFromStreamAsync加载到blob。

这是它的基本观点。 〜干杯

相关问题