C# 在asp.net中调整图像大小而不丢失图像质量?

C# 在asp.net中调整图像大小而不丢失图像质量?,c#,asp.net,C#,Asp.net,我正在开发一个ASP.NET web应用程序,允许用户上载jpeg、gif、bmp或png图像。如果上载的图像大小大于1MB。然后我想调整图像大小到15-16 Kb,间隔为230*300,而不丢失图像质量。调整图像大小后,230*300间隔的图像应该可以正常观看,请任何人帮助我 protected void btnSubmit_Click(object sender, EventArgs e) { if (IsValid) { Bitmap image = Res

我正在开发一个ASP.NET web应用程序,允许用户上载jpeg、gif、bmp或png图像。如果上载的图像大小大于1MB。然后我想调整图像大小到15-16 Kb,间隔为230*300,而不丢失图像质量。调整图像大小后,230*300间隔的图像应该可以正常观看,请任何人帮助我

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (IsValid)
    {
        Bitmap image = ResizeImage(fileImage.PostedFile.InputStream, 400, 800);
        image.Save(Server.MapPath("~/Photos/") + Guid.NewGuid().ToString() + ".jpg", ImageFormat.Jpeg);

        image.Dispose();
    }
}

private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
{
    Bitmap originalImage = new Bitmap(streamImage);
    int newWidth = originalImage.Width;
    int newHeight = originalImage.Height;
    double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

    if (aspectRatio <= 1 && originalImage.Width > maxWidth)
    {
        newWidth = maxWidth;
        newHeight = (int)Math.Round(newWidth / aspectRatio);
    }
    else if (aspectRatio > 1 && originalImage.Height > maxHeight)
    {
        newHeight = maxHeight;
        newWidth = (int)Math.Round(newHeight * aspectRatio);
    }

    Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);

    Graphics g = Graphics.FromImage(newImage);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
    g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);

    originalImage.Dispose();

    return newImage;     
}
protectedvoid btnSubmit\u单击(对象发送方,事件参数e)
{
如果(有效)
{
位图图像=大小图像(fileImage.PostedFile.InputStream,400800);
image.Save(Server.MapPath(“~/Photos/”)+Guid.NewGuid().ToString()+“.jpg”,ImageFormat.Jpeg);
image.Dispose();
}
}
私有位图大小图像(streamImage、int maxWidth、int maxHeight)
{
位图原始图像=新位图(streamImage);
int newWidth=原始图像宽度;
int newHeight=原始图像高度;
双纵横比=(双)原始图像.宽度/(双)原始图像.高度;
if(aspectRatio maxWidth)
{
newWidth=maxWidth;
newHeight=(int)Math.Round(newWidth/aspectRatio);
}
else if(aspectRatio>1&&originalImage.Height>maxHeight)
{
newHeight=maxHeight;
newWidth=(int)Math.Round(newHeight*aspectRatio);
}
位图newImage=新位图(原始图像、新宽度、新高度);
Graphics g=Graphics.FromImage(新图像);
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQuality双线性;
g、 DrawImage(原始图像,0,0,newImage.Width,newImage.Height);
原始图像处理();
返回新图像;
}

此代码曾用于我的项目,请参考以下代码

private Image RezizeI(Image img, int maxW, int maxH)
{
if(img.Height < maxH && img.Width < maxW) return img;
using (img)
{
    Double xRatio = (double)img.Width / maxW;
    Double yRatio = (double)img.Height / maxH;
    Double ratio = Math.Max(xRatio, yRatio);
    int nnx = (int)Math.Floor(img.Width / ratio);
    int nny = (int)Math.Floor(img.Height / ratio);
    Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
    using (Graphics gr = Graphics.FromImage(cpy))
    {
        gr.Clear(Color.Transparent);

        // This is said to give best quality when resizing images
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;

        gr.DrawImage(img,
            new Rectangle(0, 0, nnx, nny),
            new Rectangle(0, 0, img.Width, img.Height),
            GraphicsUnit.Pixel);
    }
    return cpy;
 }
 }
 private MemoryStream BytearrayToStream(byte[] arr)
{
  return new MemoryStream(arr, 0, arr.Length);
}

 private void HandleImageUpload(byte[] binaryImage)
{
Image img = RezizeI(Image.FromStream(BytearrayToStream(binaryImage)), 103, 32);
img.Save("Test.png", System.Drawing.Imaging.ImageFormat.Gif);
}
private Image-RezizeI(图像img、int-maxW、int-maxH)
{
if(img.Height
那么你的代码是做什么的呢?怎么了?请看那个