2015-12-27 2 views
0

Я пытаюсь изменить размер изображений. Этот код приводит к изображениям с оригинальными размерами (без изменения размера).Изображение изменяет размер изображения с исходными размерами

Image original = Image.FromFile(Server.MapPath("~/SavedFiles/Audios/") + fileNameF); 
Image resized = ResizeImage(original, new Size(200, 200)); 
MemoryStream memStream = new MemoryStream(); 
resized.Save(memStream, ImageFormat.Jpeg); 

На кнопку мыши, я называю этот код:

public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true) 
{ 
    int newWidth; 
    int newHeight; 

    if (preserveAspectRatio) 
    { 
     int originalWidth = image.Width; 
     int originalHeight = image.Height; 
     float percentWidth = (float)size.Width/(float)originalWidth; 
     float percentHeight = (float)size.Height/(float)originalHeight; 
     float percent = percentHeight < percentWidth ? percentHeight : percentWidth; 

     newWidth = (int)(originalWidth * percent); 
     newHeight = (int)(originalHeight * percent); 
    } 
    else 
    { 
     newWidth = size.Width; 
     newHeight = size.Height; 
    } 

    Image newImage = new Bitmap(newWidth, newHeight); 

    using (Graphics graphicsHandle = Graphics.FromImage(image)) 
    { 
     graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     graphicsHandle.DrawImage(newImage, 0, 0, newWidth, newHeight); 
    } 

    return newImage; 
} 
+1

'C резкое изображение в отставку, имеющий issue' в чем проблема? 'c резкое удаление изображения не работает должным образом' Как вы ожидаете, что он будет запущен, и как он работает? – Ian

+0

выше сохраняет существующие изображения с оригинальными размерами. –

+0

".. сохраняет существующие изображения ..". Я не знаю об измерениях, но из того, что вижу, он сохраняет ** пустые ** изображения. –

ответ

2

Это как я это делаю на основе кода, я тестировал использует мой образ в файл и найдите его в указанном папка тоже. Он работает нормально. Ваш код только, но небольшие проблемы (см мои комментарии по потоку и особенно в newImage декларации):

private void button1_Click(object sender, EventArgs e) { 
     Image original = Image.FromFile(Application.StartupPath + "\\ChessSet_Orig.JPG"); 
     Image resized = ResizeImage(original, new Size(200, 200));   
     FileStream fileStream = new FileStream(Application.StartupPath + "\\ChessSet_resized.JPG", FileMode.Create); //I use file stream instead of Memory stream here 
     resized.Save(fileStream, ImageFormat.Jpeg); 
     fileStream.Close(); //close after use 
    } 

    public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true) { 
     int newWidth; 
     int newHeight; 
     if (preserveAspectRatio) { 
      int originalWidth = image.Width; 
      int originalHeight = image.Height; 
      float percentWidth = (float)size.Width/(float)originalWidth; 
      float percentHeight = (float)size.Height/(float)originalHeight; 
      float percent = percentHeight < percentWidth ? percentHeight : percentWidth; 
      newWidth = (int)(originalWidth * percent); 
      newHeight = (int)(originalHeight * percent); 
     } else { 
      newWidth = size.Width; 
      newHeight = size.Height; 
     } 
     Image newImage = new System.Drawing.Bitmap(image, newWidth, newHeight); // I specify the new image from the original together with the new width and height 
     using (Graphics graphicsHandle = Graphics.FromImage(image)) { 
      graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      graphicsHandle.DrawImage(newImage, 0, 0, newWidth, newHeight); 
     } 
     return newImage; 
    } 

Вот результат,

enter image description here

Оригинальный размер изображения 820 х 760

enter image description here

+0

Большое спасибо Ian. U спас мою жизнь –

Смежные вопросы