在ASP.NET中调整图像大小--作为已调整大小的图像流输出
我从中获得了这段代码,并对其进行了一些修改,因为我想将其与我的AJAX上传程序一起使用,该上传程序需要一个流,用于将上传的项目添加到附件显示中在ASP.NET中调整图像大小--作为已调整大小的图像流输出,asp.net,ajax,image,resize,uploader,Asp.net,Ajax,Image,Resize,Uploader,我从中获得了这段代码,并对其进行了一些修改,因为我想将其与我的AJAX上传程序一起使用,该上传程序需要一个流,用于将上传的项目添加到附件显示中 public Stream ResizeFromStream(int MaxSideSize, Stream Buffer) { int intNewWidth; int intNewHeight; System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Bu
public Stream ResizeFromStream(int MaxSideSize, Stream Buffer)
{
int intNewWidth;
int intNewHeight;
System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);
// GET IMAGE FORMAT
ImageFormat fmtImageFormat = imgInput.RawFormat;
// GET ORIGINAL WIDTH AND HEIGHT
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;
// IS LANDSCAPE OR PORTRAIT ??
int intMaxSide;
if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
// SET NEW WIDTH AND HEIGHT
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
// CREATE NEW BITMAP
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
// SAVE BITMAP TO STREAM
MemoryStream imgStream = new MemoryStream();
bmpResized.Save(imgStream, imgInput.RawFormat);
// RELEASE RESOURCES
imgInput.Dispose();
bmpResized.Dispose();
Buffer.Close();
return imgStream;
}
在这段代码中被调用
private void ItemPicture_FileUploaded(object sender, UploaderEventArgs args)
{
if (GetVisibleItemCount() >= 5)
return;
using (System.IO.Stream stream = args.OpenStream())
{
ImageResize ir = new ImageResize();
// This returns a 0 byte stream
ItemPictureAttachments.Upload(args.FileSize, args.FileName, ir.ResizeFromStream(640, stream));
// This works fine
// ItemPictureAttachments.Items.Add(args.FileSize, args.FileName, stream);
}
}
我将流返回到调用它的位置是不是做错了?谢谢 根据您的代码,一切看起来都正常。我建议你把一个断点放在
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
检查imgInput是否为空。或者ImageRawFormat有问题 我使用常规ASP.NET文件控件的PostedFile.Inputstream属性测试了ResizeFromStream方法,效果很好。也许问题出在用于检索文件流的组件(args.OpenStream())上?是的,我只使用常规上载。谢谢大家!:)你有没有考虑过使用它来获得更好的图像质量和质量?您可以用
ImageBuilder.Current.Build(httpPostedFile,“file.jpg”,新的大小设置(“width=value&height=value”))替换所有代码代码>。