首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP.NET中调整图像大小--输出为调整大小的图像的流

在ASP.NET中调整图像大小--输出为调整大小的图像的流
EN

Stack Overflow用户
提问于 2009-08-20 06:11:46
回答 2查看 1.8K关注 0票数 1

我从link text获得了这段代码,并对其进行了一些修改,因为我想在我的AJAX Uploader中使用它,它需要一个流来将上传的项目添加到附件显示中;

代码语言:javascript
复制
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;
} 

并在这段代码中被调用;

代码语言:javascript
复制
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);
    }
}

我把流返回到调用它的地方是不是做错了?谢谢!

EN

Stack Overflow用户

发布于 2009-08-20 10:53:13

我使用常规ASP.NET文件控件的PostedFile.Inputstream属性测试了您的ResizeFromStream方法,它工作得很好。也许问题出在用来检索文件流的组件(args.OpenStream())?

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1304256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档