在图像上添加徽标的规模越来越大

时间:2015-07-29 03:40:10

标签: c# .net

我在用户指定的图片上添加了徽标。我的用户上传的图片大小为52KB,徽标图片大小为2KB。但当它最终保存为新图像时,它的大小变为162KB。任何人都可以帮助优化它的大小。

下面是代码,我正在使用:

public static string UploadAdPhoto(HttpContext cont,HttpPostedFileBase fileUpload, string AdId, string newfileName)
    {
        string path = cont.Server.MapPath("~/Resources/AdImages/" + AdId);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string extension = ".jpeg";
        newfileName = newfileName + extension;
        string fileName = Path.Combine(cont.Server.MapPath("~/Resources/TempImages/"), newfileName);



        fileUpload.SaveAs(fileName);
        //
        Bitmap baseImage = new System.Drawing.Bitmap(fileName);
        //if (baseImage.Width > 720 || baseImage.Height > 540)
        //{
        //    fileName = resizeImageAndSave(fileName, baseImage.Width, baseImage.Height);
        //    baseImage = new System.Drawing.Bitmap(fileName);
        //}
        //Bitmap overlayImage = (Bitmap)Image.FromFile(HttpContext.Current.Server.MapPath("~/assets/images/small_logo.png"));
        Bitmap overlayImage = (Bitmap)Image.FromFile(cont.Server.MapPath("~/assets/images/logo-signature.png"));
        Bitmap finalImage = new Bitmap(baseImage, baseImage.Width, baseImage.Height);
        var graphics = Graphics.FromImage(finalImage);
        graphics.CompositingMode = CompositingMode.SourceOver;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.DrawImage(overlayImage,0,0, 20, 20);
        System.Drawing.Imaging.Encoder myEncoder =  System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
        myEncoderParameters.Param[0] = myEncoderParameter;
        finalImage.Save(path + "/" + newfileName, jgpEncoder, myEncoderParameters);
        baseImage.Dispose();
        finalImage.Dispose();
        return "~/Resources/AdImages/" + AdId + "/" + newfileName;
    }

1 个答案:

答案 0 :(得分:1)

您是否尝试过调整大小?

        private void ResizeImage()
        {
            int iQuality = 70; //define your image quality
            string tempFileName;

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                tempFileName = "resizedImage.jpg";

                long lengthInk = 0;
                do
                {
                    var encoder = new JpegBitmapEncoder();
                    encoder.QualityLevel = iQuality;
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));

                    using (var stream1 = new FileStream(tempFileName, FileMode.Create))
                    {
                        encoder.Save(stream1);
                    }

                    FileInfo fInf = new FileInfo(tempFileName);
                    lengthInk = fInf.Length;
                    iQuality = iQuality - 5; //quality will continue to decrease by 5 until you reach the image size you want
                } while (lengthInk > 4028); //specify the maximum image size you want to have, for example 4028 for 4KB
            }
        } 

您需要根据需要更改编码器质量级别和大小。