Bala4 . V Ответов: 1

Уменьшите размер файла изображения до 150 КБ


Привет друзья

Я разрабатываю проект изменения размера изображения
мой клиент хочет, чтобы размер изображения был 240*320, а размер-50-150 КБ

но размер не контролируется размер существует более 150 КБ для изображения более 1 МБ

мой код таков:
protected void uploadButton_Click(object sender, EventArgs e)
    {
       
        Bitmap bmp1 = new Bitmap(fileUpload.FileContent);
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

        // Create an Encoder object based on the GUID 
        // for the Quality parameter category.
        System.Drawing.Imaging.Encoder myEncoder =
            System.Drawing.Imaging.Encoder.Quality;

        // Create an EncoderParameters object. 
        // An EncoderParameters object has an array of EncoderParameter 
        // objects. In this case, there is only one 
        // EncoderParameter object in the array.
        EncoderParameters myEncoderParameters = new EncoderParameters(1);

        //EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);

        //myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

        // Save the bitmap as a JPG file with zero quality level compression.
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);



        if (fileUpload.HasFile)
        {
            // Find the fileUpload control
            string filename = fileUpload.FileName;

            // Check if the directory we want the image uploaded to actually exists or not
            if (!Directory.Exists(MapPath(@"Uploaded-Files")))
            {
                // If it doesn't then we just create it before going any further
                Directory.CreateDirectory(MapPath(@"Uploaded-Files"));
            }

            // Specify the upload directory
            string directory = Server.MapPath(@"Uploaded-Files\");

            // Create a bitmap of the content of the fileUpload control in memory
            Bitmap originalBMP = new Bitmap(@"c:\TestPhotoQualityZero.jpg");

            // Calculate the new image dimensions
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int sngRatio = origWidth / origHeight;
            int newWidth = 240;
            //int newHeight = newWidth / sngRatio;
            int newHeight = 320;

            // Create a new bitmap which will hold the previous resized bitmap
            Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
            // Create a graphic based on the new bitmap
            Graphics oGraphics = Graphics.FromImage(newBMP);

            // Set the properties for the new graphic file
            oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            // Draw the new graphic based on the resized bitmap
            oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);


            // Save the new graphic file to the server
            newBMP.Save(directory + "tn_" + filename);

            // Once finished with the bitmap objects, we deallocate them.
            originalBMP.Dispose();
            newBMP.Dispose();
            oGraphics.Dispose();

            // Write a message to inform the user all is OK
            label.Text = "File Name: 255, 0, 0, 1)">" + filename + "<br>";
            label.Text += "Content Type: 255, 0, 0, 1)">" + fileUpload.PostedFile.ContentType + "<br>";
            label.Text += "File Size: 255, 0, 0, 1)">" + fileUpload.PostedFile.ContentLength.ToString() + "";
            // Display the image to the user
            Image1.Visible = true;
            Image1.ImageUrl = @"/Uploaded-Files/tn_" + filename;
        }
        else
        {
            label.Text = "No file uploaded!";
        }
    }

    private ImageCodecInfo GetEncoder(ImageFormat format)
    {

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

1 Ответов

Рейтинг:
2

Maciej Los

Пожалуйста, используйте Окнопоиска[^] (73 страницы!).