SORU
21 Aralık 2009, PAZARTESİ


c# en-boy oranını koruyarak farklı bir boyuta yeniden boyutlandırma Görüntü

Yeni görüntü ezilmiş görünmüyor böylece orijinal resim en boy oranını koruyarak görüntüyü yeniden boyutlandırmak için çalışıyorum.

örneğin:

150*150 bir görüntü içine 150*100 bir resim dönüştürmek.
Yüksekliği 50 piksel beyaz bir arka plan rengi ile yastıklı olması gerekir.

Bu kullanarak benim geçerli kod.

İyi en-boy oranını yeni görüntü yeniden boyutlandırma eziyor ama değiştirmek için çalışıyor.

private void resizeImage(string path, string originalFilename, 
                         int width, int height)
    {
        Image image = Image.FromFile(path   originalFilename);

        System.Drawing.Image thumbnail = new Bitmap(width, height);
        System.Drawing.Graphics graphic = 
                     System.Drawing.Graphics.FromImage(thumbnail);

        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = CompositingQuality.HighQuality;

        graphic.DrawImage(image, 0, 0, width, height);

        System.Drawing.Imaging.ImageCodecInfo[] info =
                         ImageCodecInfo.GetImageEncoders();
        EncoderParameters encoderParameters;
        encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                         100L);            
        thumbnail.Save(path   width   "."   originalFilename, info[1], 
                         encoderParameters);
    }

EDİT: resim yerine yastıklı kırpılmış almak istiyorum

CEVAP
4 Ocak 2010, PAZARTESİ


Bunun işe yaraması lazım.

private void resizeImage(string path, string originalFilename, 
                     /* note changed names */
                     int canvasWidth, int canvasHeight, 
                     /* new */
                     int originalWidth, int originalHeight)
{
    Image image = Image.FromFile(path   originalFilename);

    System.Drawing.Image thumbnail = 
        new Bitmap(canvasWidth, canvasHeight); // changed parm names
    System.Drawing.Graphics graphic = 
                 System.Drawing.Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    /* ------------------ new code --------------- */

    // Figure out the ratio
    double ratioX = (double) canvasWidth / (double) originalWidth;
    double ratioY = (double) canvasHeight / (double) originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    // Now calculate the X,Y position of the upper-left corner 
    // (one of these will always be zero)
    int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
    int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);

    graphic.Clear(Color.White); // white padding
    graphic.DrawImage(image, posX, posY, newWidth, newHeight);

    /* ------------- end new code ---------------- */

    System.Drawing.Imaging.ImageCodecInfo[] info =
                     ImageCodecInfo.GetImageEncoders();
    EncoderParameters encoderParameters;
    encoderParameters = new EncoderParameters(1);
    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                     100L);            
    thumbnail.Save(path   newWidth   "."   originalFilename, info[1], 
                     encoderParameters);
}

Düzenlenmiş eklemek için:

Bu kodu geliştirmek isteyenler yorum koymak, ya da yeni bir cevap vermeli. Bu kodu doğrudan bir düzenleme yok.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • rekjavicxxx

    rekjavicxxx

    28 EKİM 2007
  • The CGBros

    The CGBros

    20 AĞUSTOS 2011
  • thegeniuses.tv

    thegeniuses.

    11 Aralık 2006