有没有方便的方法用C#调整图像大小?
答案 0 :(得分:1)
以下链接可以帮助您:)
snippets.dzone.com/posts/show/4336
更新:如果您打算调整图像文件大小以节省空间,上面链接中的代码会实际调整大小并保存图像,否则如上所述,您只需手动设置图像控件上的高度/宽度。 / p>
答案 1 :(得分:0)
如果您加载图像,则只需设置宽度/高度属性。
e.g。
Bitmap bmp = new Bitmap(@"c:\myfile.bmp");
bmp.Width = (int)(bmp.Width / 2);
bmp.Height = (int)(bmp.Height / 2);
答案 2 :(得分:0)
将其加载到gdi +(system.drawing)并缩放?
答案 3 :(得分:0)
答案 4 :(得分:0)
除了其他人所说的,对于小于300x300的图像,Image.GetThumbnailImage非常方便:
static void Main(string[] args)
{
Bitmap b = new Bitmap(@"C:\Documents and Settings\juliet\My Documents\My Pictures\julietawesome.bmp");
Image image = b.GetThumbnailImage(100, 100, null, IntPtr.Zero);
image.Save(@"C:\Documents and Settings\juliet\My Documents\My Pictures\thumbnail.bmp", ImageFormat.Bmp);
}
答案 5 :(得分:0)
由于没有其他人这样说过:尝试WPF。
答案 6 :(得分:0)
Public Function ScaleByPercent(ByVal imgPhoto As Image, ByVal Percent As Integer) As Image
Dim nPercent As Single = (CType(Percent, Single) / 100)
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0
Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
Dim bmPhoto As New Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)
grPhoto.Dispose()
Return bmPhoto
End Function
答案 7 :(得分:-2)
来自我的个人代码库。反正有点老了。
protected Image FixedSize(Image imgPhoto, int Width, int Height)
{
int width = imgPhoto.Width;
int height = imgPhoto.Height;
int x = 0;
int y = 0;
int num5 = 0;
int num6 = 0;
float num7 = 0f;
float num8 = 0f;
float num9 = 0f;
num8 = ((float) Width) / ((float) width);
num9 = ((float) Height) / ((float) height);
if (num9 < num8)
{
num7 = num9;
num5 = Convert.ToInt16((float) ((Width - (width * num7)) / 2f));
}
else
{
num7 = num8;
num6 = Convert.ToInt16((float) ((Height - (height * num7)) / 2f));
}
int num10 = (int) (width * num7);
int num11 = (int) (height * num7);
Bitmap image = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
image.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(ColorTranslator.FromHtml("#ffffff"));
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(imgPhoto, new Rectangle(num5, num6, num10, num11), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
graphics.Dispose();
return image;
}