如何在C#中像IOS方法一样拉伸图像
<div class="c">
<div id="shiva"><span class="count">300</span></div>
<div id="shiva"><span class="count">78</span></div>
<div id="shiva"><span class="count">32</span></div>
</div>
.c {
width: 350px;
margin:0 auto;
}
呢?
答案 0 :(得分:0)
我认为你需要调整图片大小吗?请检查以下代码:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
/// <summary>
/// Class contaning method to resize an image and save in JPEG format
/// </summary>
public class ImageHandler
{
public void Save(Bitmap image, int maxWidth, int maxHeight, int quality, string filePath)
{
// Get the image's original width and height
int originalWidth = image.Width;
int originalHeight = image.Height;
// To preserve the aspect ratio
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
// New width and height based on aspect ratio
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
// Convert other formats (including CMYK) to RGB.
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
// Draws the image in the specified size with quality mode set to HighQuality
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
// Get an ImageCodecInfo object that represents the JPEG codec.
ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Jpeg);
// Create an Encoder object for the Quality parameter.
Encoder encoder = Encoder.Quality;
// Create an EncoderParameters object.
EncoderParameters encoderParameters = new EncoderParameters(1);
// Save the image as a JPEG file with quality level.
EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
encoderParameters.Param[0] = encoderParameter;
newImage.Save(filePath, imageCodecInfo, encoderParameters);
}
/// <summary>
/// Method to get encoder infor for given image format.
/// </summary>
/// <param name="format">Image format</param>
/// <returns>image codec info.</returns>
private ImageCodecInfo GetEncoderInfo(ImageFormat format)
{
return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
}
}