Как изменить размер изображения С#
Поскольку Size
, Width
и Height
являются свойствами Get()
System.Drawing.Image
;
Как изменить размер объекта изображения во время выполнения на С#?
Прямо сейчас, я просто создаю новый Image
используя:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
Ответы
Ответ 1
Это приведет к изменению размера изображения высокого качества:
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
-
wrapMode.SetWrapMode(WrapMode.TileFlipXY)
предотвращает ореолы вокруг границ изображения - наивное изменение размера будет отображать прозрачные пиксели за пределами границ изображения, но, отражая изображение, мы можем получить лучший образец (этот параметр очень заметен)
-
destImage.SetResolution
поддерживает DPI независимо от физического размера - может повысить качество при уменьшении размеров изображения или при печати
- Элементы управления компоновкой, как пиксели смешиваются с фоном, могут не понадобиться, поскольку мы рисуем только одну вещь.
-
graphics.InterpolationMode
определяет, как рассчитываются промежуточные значения между двумя конечными точками.
-
graphics.SmoothingMode
указывает, используют ли линии, кривые и края заполненных областей сглаживание (также называемое сглаживание) - возможно, работает только на векторах
-
graphics.PixelOffsetMode
влияет на качество рендеринга при рисовании нового изображения
Сохранение пропорций остается как упражнение для читателя (на самом деле, я просто не думаю, что это служебное задание для вас).
Кроме того, это хорошая статья, описывающая некоторые подводные камни с изменением размера изображения. Вышеупомянутая функция будет охватывать большинство из них, но вам все равно придется беспокоиться о сохранении.
Ответ 2
Не уверен, что это так сложно, сделайте то, что вы делали, используйте перегруженный конструктор Bitmap для создания изображения с размерами, единственное, что вам не хватало, - это возврат к типу данных изображения:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(50,50));
Ответ 3
в этот вопрос, у вас появятся ответы, в том числе мои:
public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
imgPhoto.Dispose();
return bmPhoto;
}
Ответ 4
Почему бы не использовать метод System.Drawing.Image.GetThumbnailImage
?
public Image GetThumbnailImage(
int thumbWidth,
int thumbHeight,
Image.GetThumbnailImageAbort callback,
IntPtr callbackData)
Пример:
Image originalImage = System.Drawing.Image.FromStream(inputStream, true, true);
Image resizedImage = originalImage.GetThumbnailImage(newWidth, (newWidth * originalImage.Height) / originalWidth, null, IntPtr.Zero);
resizedImage.Save(imagePath, ImageFormat.Png);
Источник:
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
Ответ 5
Это будет
- Изменение ширины и высоты без необходимости в цикле
- Не превышает исходные размеры изображений
//////////////
private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
double resizeWidth = img.Source.Width;
double resizeHeight = img.Source.Height;
double aspect = resizeWidth / resizeHeight;
if (resizeWidth > maxWidth)
{
resizeWidth = maxWidth;
resizeHeight = resizeWidth / aspect;
}
if (resizeHeight > maxHeight)
{
aspect = resizeWidth / resizeHeight;
resizeHeight = maxHeight;
resizeWidth = resizeHeight * aspect;
}
img.Width = resizeWidth;
img.Height = resizeHeight;
}
Ответ 6
public static Image resizeImage(Image image, int new_height, int new_width)
{
Bitmap new_image = new Bitmap(new_width, new_height);
Graphics g = Graphics.FromImage((Image)new_image );
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(image, 0, 0, new_width, new_height);
return new_image;
}
Ответ 7
В приложении, которое я сделал, необходимо было создать функцию с несколькими параметрами. Он довольно большой, но он изменяет размер изображения, может сохранять соотношение сторон и может вырезать края, чтобы вернуть только центр изображения:
/// <summary>
/// Resize image with a directory as source
/// </summary>
/// <param name="OriginalFileLocation">Image location</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <param name="keepAspectRatio">keep the aspect ratio</param>
/// <param name="getCenter">return the center bit of the image</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width, Boolean keepAspectRatio, Boolean getCenter)
{
int newheigth = heigth;
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFileLocation);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (keepAspectRatio || getCenter)
{
int bmpY = 0;
double resize = (double)FullsizeImage.Width / (double)width;//get the resize vector
if (getCenter)
{
bmpY = (int)((FullsizeImage.Height - (heigth * resize)) / 2);// gives the Y value of the part that will be cut off, to show only the part in the center
Rectangle section = new Rectangle(new Point(0, bmpY), new Size(FullsizeImage.Width, (int)(heigth * resize)));// create the section to cut of the original image
//System.Console.WriteLine("the section that will be cut off: " + section.Size.ToString() + " the Y value is minimized by: " + bmpY);
Bitmap orImg = new Bitmap((Bitmap)FullsizeImage);//for the correct effect convert image to bitmap.
FullsizeImage.Dispose();//clear the original image
using (Bitmap tempImg = new Bitmap(section.Width, section.Height))
{
Graphics cutImg = Graphics.FromImage(tempImg);// set the file to save the new image to.
cutImg.DrawImage(orImg, 0, 0, section, GraphicsUnit.Pixel);// cut the image and save it to tempImg
FullsizeImage = tempImg;//save the tempImg as FullsizeImage for resizing later
orImg.Dispose();
cutImg.Dispose();
return FullsizeImage.GetThumbnailImage(width, heigth, null, IntPtr.Zero);
}
}
else newheigth = (int)(FullsizeImage.Height / resize);// set the new heigth of the current image
}//return the image resized to the given heigth and width
return FullsizeImage.GetThumbnailImage(width, newheigth, null, IntPtr.Zero);
}
Чтобы облегчить доступ к функции, можно добавить некоторые перегруженные функции:
/// <summary>
/// Resize image with a directory as source
/// </summary>
/// <param name="OriginalFileLocation">Image location</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width)
{
return resizeImageFromFile(OriginalFileLocation, heigth, width, false, false);
}
/// <summary>
/// Resize image with a directory as source
/// </summary>
/// <param name="OriginalFileLocation">Image location</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <param name="keepAspectRatio">keep the aspect ratio</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width, Boolean keepAspectRatio)
{
return resizeImageFromFile(OriginalFileLocation, heigth, width, keepAspectRatio, false);
}
Теперь это два последних логических параметра, которые необходимо установить.
Вызовите функцию следующим образом:
System.Drawing.Image ResizedImage = resizeImageFromFile(imageLocation, 800, 400, true, true);
Ответ 8
Этот код такой же, как и в одном из приведенных выше ответов.. но преобразует прозрачный пиксель в белый вместо черного... Спасибо:)
public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
imgPhoto.Dispose();
return bmPhoto;
}
Ответ 9
Это код, который я разработал для конкретного требования, т.е. место назначения всегда в ландшафтном отношении. Это должно дать вам хорошее начало.
public Image ResizeImage(Image source, RectangleF destinationBounds)
{
RectangleF sourceBounds = new RectangleF(0.0f,0.0f,(float)source.Width, (float)source.Height);
RectangleF scaleBounds = new RectangleF();
Image destinationImage = new Bitmap((int)destinationBounds.Width, (int)destinationBounds.Height);
Graphics graph = Graphics.FromImage(destinationImage);
graph.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// Fill with background color
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), destinationBounds);
float resizeRatio, sourceRatio;
float scaleWidth, scaleHeight;
sourceRatio = (float)source.Width / (float)source.Height;
if (sourceRatio >= 1.0f)
{
//landscape
resizeRatio = destinationBounds.Width / sourceBounds.Width;
scaleWidth = destinationBounds.Width;
scaleHeight = sourceBounds.Height * resizeRatio;
float trimValue = destinationBounds.Height - scaleHeight;
graph.DrawImage(source, 0, (trimValue / 2), destinationBounds.Width, scaleHeight);
}
else
{
//portrait
resizeRatio = destinationBounds.Height/sourceBounds.Height;
scaleWidth = sourceBounds.Width * resizeRatio;
scaleHeight = destinationBounds.Height;
float trimValue = destinationBounds.Width - scaleWidth;
graph.DrawImage(source, (trimValue / 2), 0, scaleWidth, destinationBounds.Height);
}
return destinationImage;
}
Ответ 10
public string CreateThumbnail(int maxWidth, int maxHeight, string path)
{
var image = System.Drawing.Image.FromFile(path);
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics thumbGraph = Graphics.FromImage(newImage);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
//thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
image.Dispose();
string fileRelativePath = "newsizeimages/" + maxWidth + Path.GetFileName(path);
newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
return fileRelativePath;
}
Нажмите здесь http://bhupendrasinghsaini.blogspot.in/2014/07/resize-image-in-c.html
Ответ 11
Вы можете попробовать net-vips, привязку С# для libvips. Это ленивая, потоковая, управляемая по требованию библиотека обработки изображений, поэтому она может выполнять такие операции без необходимости загружать все изображение.
Например, он поставляется с удобным приложением для просмотра изображений:
Image image = Image.Thumbnail("image.jpg", 300, 300);
image.WriteToFile("my-thumbnail.jpg");
Он также поддерживает интеллектуальное кадрирование, способ интеллектуального определения наиболее важной части изображения и удержания его в фокусе при кадрировании изображения. Например:
Image image = Image.Thumbnail("owl.jpg", 128, crop: "attention");
image.WriteToFile("tn_owl.jpg");
Где owl.jpg
- нецентральная композиция:
![Owl]()
Дает этот результат:
![Owl smart crop]()
Сначала оно сжимает изображение, чтобы получить вертикальную ось до 128 пикселей, а затем обрезается до 128 пикселей с использованием стратегии attention
. Эта функция ищет на изображении объекты, которые могут привлечь внимание человека, подробности см. В Smartcrop()
.
Ответ 12
Если вы работаете с BitmapSource
:
var resizedBitmap = new TransformedBitmap(
bitmapSource,
new ScaleTransform(scaleX, scaleY));
Если вы хотите более тонкое управление качеством, сначала выполните это:
RenderOptions.SetBitmapScalingMode(
bitmapSource,
BitmapScalingMode.HighQuality);
(По умолчанию используется BitmapScalingMode.Linear
, что эквивалентно BitmapScalingMode.LowQuality
.)
Ответ 13
Я использую ImageProcessorCore, в основном потому, что он работает .Net Core.
И у него есть больше возможностей, таких как типы конвертирования, обрезка изображений и многое другое
http://imageprocessor.org/imageprocessor/
Ответ 14
Измените размер и сохраните изображение в соответствии с шириной и высотой, как изображение, сохраняющее изображение пропорционально
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace Infra.Files
{
public static class GenerateThumb
{
/// <summary>
/// Resize and save an image to fit under width and height like a canvas keeping things proportional
/// </summary>
/// <param name="originalImagePath"></param>
/// <param name="thumbImagePath"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
public static void GenerateThumbImage(string originalImagePath, string thumbImagePath, int newWidth, int newHeight)
{
Bitmap srcBmp = new Bitmap(originalImagePath);
float ratio = 1;
float minSize = Math.Min(newHeight, newHeight);
if (srcBmp.Width > srcBmp.Height)
{
ratio = minSize / (float)srcBmp.Width;
}
else
{
ratio = minSize / (float)srcBmp.Height;
}
SizeF newSize = new SizeF(srcBmp.Width * ratio, srcBmp.Height * ratio);
Bitmap target = new Bitmap((int)newSize.Width, (int)newSize.Height);
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(srcBmp, 0, 0, newSize.Width, newSize.Height);
using (MemoryStream memoryStream = new MemoryStream())
{
target.Save(thumbImagePath);
}
}
}
}
}
Ответ 15
Для этого вы можете использовать Accord.NET framework. Он предоставляет несколько различных способов изменения размера:
Ответ 16
Примечание: это не будет работать с ASP.Net Core, потому что WebImage зависит от System.Web, но в предыдущих версиях ASP.Net я использовал этот фрагмент много раз и был полезен.
String ThumbfullPath = Path.GetFileNameWithoutExtension(file.FileName) + "80x80.jpg";
var ThumbfullPath2 = Path.Combine(ThumbfullPath, fileThumb);
using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(fullPath)))
{
var thumbnail = new WebImage(stream).Resize(80, 80);
thumbnail.Save(ThumbfullPath2, "jpg");
}
Ответ 17
Используйте функцию ниже с примером ниже для изменения размера изображения:
//Example :
System.Net.Mime.MediaTypeNames.Image newImage = System.Net.Mime.MediaTypeNames.Image.FromFile("SampImag.jpg");
System.Net.Mime.MediaTypeNames.Image temImag = FormatImage(newImage, 100, 100);
//image size modification unction
public static System.Net.Mime.MediaTypeNames.Image FormatImage(System.Net.Mime.MediaTypeNames.Image img, int outputWidth, int outputHeight)
{
Bitmap outputImage = null;
Graphics graphics = null;
try
{
outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
graphics = Graphics.FromImage(outputImage);
graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight),
new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
return outputImage;
}
catch (Exception ex)
{
return img;
}
}
Ответ 18
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
fileupload1.SaveAs(Server.MapPath("images/tempImage/" + fileupload1.FileName));
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("images/tempImage/" + fileupload1.FileName));
int newwidthimg = 200;
float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
int newHeight = 200;
Bitmap bitMAP1 = new Bitmap(newwidthimg, newHeight);
Graphics imgGraph = Graphics.FromImage(bitMAP1);
bitMAP1.imgQuality = CompositingQuality.HighQuality;
bitMAP1.SmoothingMode = SmoothingMode.HighQuality;
bitMAP1.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imgDimesions = new Rectangle(0, 0, newwidthimg, newHeight);
bitMAP1.DrawImage(image, imageRectangle);
bitMAP1.Save(Server.MapPath("images/Shops/" +fileupload1.filename), ImageFormat.Jpeg);
bitMAP1.Dispose();
bitMAP1.Dispose();
image.Dispose();
if (System.IO.File.Exists(Server.MapPath("images/tempImage/" + fileupload1.FileName)))
{
System.IO.File.Delete(Server.MapPath("images/tempImage/" + fileupload1.FileName));
}
Ответ 19
Я использую этот код в своих проектах, и он отлично работает
var ms = new MemoryStream(File.ReadAllBytes(Source));
Bitmap objBitmap = new Bitmap(ms);
if (size.Height == 0)
{
size = objBitmap.Size;
}
objBitmap = new Bitmap(objBitmap, size);
objBitmap.Save(Destination);