2

我有一个文件上传控件,允许用户上传图像,但在他们上传图像之前,我想将 thomse 图像调整为 640x480 大小,问题是我不知道下一步该做什么。这就是我所拥有的;

// CALL THE FUNCTION THAT WILL RESIZE THE IMAGE
protected void btnUploadFile_Click(object sender, EventArgs e)
{
    Stream imgStream = ir.ResizeFromStream(640, fupItemImage.PostedFile.InputStream);

    // What to do next? 
}

// THE FUNCTION THAT WILL RESIZE IMAGE THEN RETURN AS MEMORY STREAM
public MemoryStream 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 imgMStream = new MemoryStream();
    bmpResized.Save(imgMStream, imgInput.RawFormat);

    // RELEASE RESOURCES
    imgInput.Dispose();
    bmpResized.Dispose();
    Buffer.Close();

    return imgMStream;
} 

谢谢

4

6 回答 6

1

也许像

int length = 256;
int bytesRead = 0;
Byte[] buffer = new Byte[length];
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
  do
  {
    bytesRead = imgStream.Read(buffer, 0, length);
    fs.Write(buffer, 0, bytesRead);
  }
  while (bytesRead == length);
}
于 2009-08-25T13:55:16.013 回答
0

我只是快速浏览了一下,但如果您已经成功执行了图像大小调整,那么您需要做的就是使用 System.IO 名称空间文件类通过写出内存中的字节将内存流保存到文件中流到您创建的 File 对象。如果您需要代码示例,请告诉我。

于 2009-08-25T13:59:40.843 回答
0
FileStream fs=new FileStream("filename",FileMode.Create);
ir.ResizeFromStream(640, fupItemImage.PostedFile.InputStream).WriteTo(fs);
fs.Close();
于 2009-08-25T14:02:48.407 回答
0

转储到文件将如下所示:

using (FileStream fsOut = new FileStream("my\\file\\path.img", FileMode.Create))
using (MemoryStream msImg = new MemoryStream(ir.ResizeFromStream(640 ...)) )
{
    byte[] imgData = msImg.ToArray();
    fsOut.Write(imgData, 0, imgData.Length);
}
于 2009-08-25T13:58:00.627 回答
0

如果您不使用返回的流,ResizeFromStream您还可以更改该方法并使其返回转换后的Bitmap. 然后使用Bitmap.Save(<path>)将图像保存为文件。

于 2009-08-25T14:14:24.880 回答
0
  1. 您当前的代码在遇到异常时会泄漏资源。您应该始终将调用.Dispose放在 finally 或 catch 块中(取决于资源是始终需要处置,还是只需要在发生错误时处置)。try...finally Dispose 可以用using关键字简洁地编写 - 省去你未来的麻烦,并尝试将所有 IDisposables 放在一个usingtry..catch块中。
  2. 默认情况下,使用 Windows 窗体调整图像大小将使用低质量的重采样器。这通常会导致输出看起来很糟糕;您需要专门设置适当的插值模式(代码见下文)。
  3. 如果您正在保存图像,您可能会遇到图像编解码器设置问题:例如,默认的 jpeg 编码器在默认情况下具有极低的输出质量。这也需要您手动设置(见下文)。
  4. 最后,将文件保存到磁盘或数据库实际上是其中最简单的部分——与其保存到内存流,不如简单地采用流参数。然后可以构造此参数以根据调用者的需要写入文件或数据库。

我使用以下函数来调整图像大小(这将改变纵横比):

public static Bitmap Resize(Image oldImage, int newWidth, int newHeight)
{
    Bitmap bitmap = null;
    try
    {
        bitmap = new Bitmap(newWidth, newHeight);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(oldImage, new Rectangle(0, 0, newWidth, newHeight));
        }
    }
    catch
    {
        if (bitmap != null) bitmap.Dispose();
        throw;
    }
    return bitmap;
}

例如,可以通过以下方式将文件保存为 jpeg:

public static void SaveImageAsJpeg(Image image, Stream outputStream, int quality)
{
    ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders()
        .Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
    using (EncoderParameters encParams = new EncoderParameters(1))
    {
        encParams.Param[0] = new EncoderParameter(Encoder.Quality,(long)quality);
        //quality should be in the range [0..100]
        image.Save(outputStream, jpgInfo, encParams);
    }
}

您可以通过以下方式使用它

using(var origImage=Image.FromStream(inputStream, true, true))
{
    //set newWidth, newHeight however you wish; determine outputFilePath
    using(var newImage = Resize(origImage, newWidth, newHeight))
    using(var outputStream = File.Open(outputFilePath, FileMode.Create))
        SaveImageAsJpeg(newImage,outputStream, 90);
}
于 2009-08-25T14:16:43.113 回答