更改流中的图像大小
Changing image size from stream
我在这里搜索过这方面的帮助,但没有任何内容完全符合我的需要。我有一张上传的图片,我想在它保存到 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 像素?
这是我的做法:
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。
这是对它的基本看法。
我在这里搜索过这方面的帮助,但没有任何内容完全符合我的需要。我有一张上传的图片,我想在它保存到 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 像素?
这是我的做法:
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。
这是对它的基本看法。