Sử dụng C# làm cách nào tôi có thể thay đổi kích thước hình ảnh jpeg? Một mẫu mã sẽ là tuyệt vời.Sử dụng C# làm cách nào tôi có thể thay đổi kích thước hình ảnh jpeg?
Trả lời
C# (hay đúng hơn là: Khuôn khổ .NET) không cung cấp khả năng như vậy, nhưng nó cung cấp cho bạn Bitmap từ System.Drawing để dễ dàng truy cập dữ liệu pixel thô của các định dạng hình ảnh khác nhau. Đối với phần còn lại, hãy xem http://en.wikipedia.org/wiki/Image_scaling
Bộ lọc và mã cỡ mẫu miễn phí tốt.
http://code.google.com/p/zrlabs-yael/
private void MakeResizedImage(string fromFile, string toFile, int maxWidth, int maxHeight)
{
int width;
int height;
using (System.Drawing.Image image = System.Drawing.Image.FromFile(fromFile))
{
DetermineResizeRatio(maxWidth, maxHeight, image.Width, image.Height, out width, out height);
using (System.Drawing.Image thumbnailImage = image.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
if (image.Width < thumbnailImage.Width && image.Height < thumbnailImage.Height)
File.Copy(fromFile, toFile);
else
{
ImageCodecInfo ec = GetCodecInfo();
EncoderParameters parms = new EncoderParameters(1);
parms.Param[0] = new EncoderParameter(Encoder.Compression, 40);
ZRLabs.Yael.BasicFilters.ResizeFilter rf = new ZRLabs.Yael.BasicFilters.ResizeFilter();
//rf.KeepAspectRatio = true;
rf.Height = height;
rf.Width = width;
System.Drawing.Image img = rf.ExecuteFilter(System.Drawing.Image.FromFile(fromFile));
img.Save(toFile, ec, parms);
}
}
}
}
Lưu ý rằng đoạn mã này không đủ, bạn phải thêm tham chiếu đến dự án (thay đổi cuối cùng là ngày 6 tháng 11 năm 2006) – itsho
đẹp ví dụ.
public static Image ResizeImage(Image sourceImage, int maxWidth, int maxHeight)
{
// Determine which ratio is greater, the width or height, and use
// this to calculate the new width and height. Effectually constrains
// the proportions of the resized image to the proportions of the original.
double xRatio = (double)sourceImage.Width/maxWidth;
double yRatio = (double)sourceImage.Height/maxHeight;
double ratioToResizeImage = Math.Max(xRatio, yRatio);
int newWidth = (int)Math.Floor(sourceImage.Width/ratioToResizeImage);
int newHeight = (int)Math.Floor(sourceImage.Height/ratioToResizeImage);
// Create new image canvas -- use maxWidth and maxHeight in this function call if you wish
// to set the exact dimensions of the output image.
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
// Render the new image, using a graphic object
using (Graphics newGraphic = Graphics.FromImage(newImage))
{
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
newGraphic.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
// Set the background color to be transparent (can change this to any color)
newGraphic.Clear(Color.Transparent);
// Set the method of scaling to use -- HighQualityBicubic is said to have the best quality
newGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Apply the transformation onto the new graphic
Rectangle sourceDimensions = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);
Rectangle destinationDimensions = new Rectangle(0, 0, newWidth, newHeight);
newGraphic.DrawImage(sourceImage, destinationDimensions, sourceDimensions, GraphicsUnit.Pixel);
}
// Image has been modified by all the references to it's related graphic above. Return changes.
return newImage;
}
Chỉ định một 'WrapMode' như trong câu trả lời của LucidObscurity g ives bạn một kết quả tốt hơn. Bạn có thể muốn thêm mã đó vào mã của mình. – Andrew
public static class ImageHelper
{
/// <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;
}
public static Bitmap ResizeImage(Image image, decimal percentage)
{
int width = (int)Math.Round(image.Width * percentage, MidpointRounding.AwayFromZero);
int height = (int)Math.Round(image.Height * percentage, MidpointRounding.AwayFromZero);
return ResizeImage(image, width, height);
}
}
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\Images\MyImage.jpg";
FileInfo info = new FileInfo(fileName);
using (Image image = Image.FromFile(fileName))
{
using(Bitmap resizedImage = ImageHelper.ResizeImage(image, 0.25m))
{
resizedImage.Save(
info.DirectoryName + "\\"
+ info.Name.Substring(0, info.Name.LastIndexOf(info.Extension))
+ "_" + resizedImage.Width + "_" + resizedImage.Height
+ info.Extension,
ImageFormat.Jpeg);
}
}
}
}
Tôi đang sử dụng này:
public static void ResizeJpg(string path, int nWidth, int nHeight)
{
using (var result = new Bitmap(nWidth, nHeight))
{
using (var input = new Bitmap(path))
{
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
g.DrawImage(input, 0, 0, nWidth, nHeight);
}
}
var ici = ImageCodecInfo.GetImageEncoders().FirstOrDefault(ie => ie.MimeType == "image/jpeg");
var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
result.Save(path, ici, eps);
}
}
- 1. Thay đổi kích thước hình ảnh jpeg thành kích thước được chỉ định
- 2. Thay đổi kích thước hình ảnh bằng cách sử dụng C#
- 3. Thay đổi kích thước hình ảnh sử dụng Jcrop
- 4. Làm cách nào để thay đổi kích thước ảnh thành một kích thước tệp cụ thể?
- 5. Thay đổi kích thước hình ảnh bitmap
- 6. UIImageView: Thay đổi kích thước thành kích thước hình ảnh?
- 7. Thay đổi kích thước hình ảnh IFrame
- 8. Thay đổi kích thước hình ảnh PNG
- 9. Thay đổi kích thước hình ảnh JPEG trong khi giải mã
- 10. thay đổi kích thước hình ảnh ckfinder
- 11. Tôi làm cách nào để thay đổi kích thước hình ảnh mà không kéo dài?
- 12. Kích thước tệp tối đa của hình ảnh JPEG có kích thước đã biết
- 13. Cách thay đổi kích thước hình ảnh UIBarButtonItem
- 14. Thay đổi kích thước hình ảnh trong Java để giảm kích thước hình ảnh
- 15. Thay đổi kích thước hình ảnh trong asp.net mà không làm mất chất lượng hình ảnh
- 16. C# lý do thay đổi kích thước hình ảnh sẽ tăng kích thước tệp
- 17. Blackberry - cách đổi kích thước hình ảnh?
- 18. ImageMagick - Thay đổi kích thước hình ảnh thành hình vuông
- 19. Thay đổi kích thước hình ảnh bằng django?
- 20. Làm thế nào tôi có thể thay đổi kích thước của recaptcha trong Greasemonkey?
- 21. thay đổi kích thước hình ảnh từ tập tin
- 22. PIL Image.resize() không thay đổi kích thước hình ảnh
- 23. Thay đổi kích thước hình ảnh theo kích thước văn bản
- 24. nhanh nhất C/C++ hình ảnh thay đổi kích thước thư viện
- 25. Cách giảm kích thước ảnh JPEG trong Android
- 26. Thay đổi kích thước hình ảnh trước khi tải lên
- 27. thay đổi kích thước hình ảnh bằng jquery
- 28. làm thế nào để tôi sử dụng imagick trong php? (thay đổi kích thước và cắt)
- 29. thay đổi kích thước hình ảnh khi lưu
- 30. iTextSharp: Cách thay đổi kích thước hình ảnh để vừa với kích thước cố định?
Xem bài đăng này: https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/ – erdinger