在C#中调整图像大小 [英] Resizing image in C#
问题描述
我写一个code来调整在C#中的JPG图片。我的code大约需要6秒调整20 JPG图片。我想知道,如果在C#这样做的任何更快的方法?改善这种任何建议是AP preciated!
下面是我的code现在:
位图bmpOrig,bmpDest,bmpOrigCopy;
的foreach(在strarrFileList串strJPGImagePath)
{
bmpOrig =新位图(strJPGImagePath);
bmpOrigCopy =新位图(bmpOrig);
bmpOrig.Dispose();
File.Delete(strJPGImagePath);bmpDest =新位图(bmpOrigCopy,新的大小(100,200));
bmpDest.Save(strJPGImagePath,jgpEn codeR,myEn coderParameters);bmpOrigCopy.Dispose();
bmpDest.Dispose();
}
感谢@Guffa他的解决方案。我搬到了Dispose()出foreach循环。更新和快速code是:
位图bmpDest =新位图(1,1);
的foreach(在strarrFileList串strJPGImagePath)
{
使用(位图bmpOrig =新位图(strJPGImagePath))
{
bmpDest =新位图(bmpOrig,新的大小(100,200));
}
bmpDest.Save(strJPGImagePath,jgpEn codeR,myEn coderParameters);
}
bmpDest.Dispose();
而不要照搬两个步骤的位图,使其一步。这样可以减少内存使用量相当多,你不具备oringal图像的两个副本在内存中一次。
的foreach(在strarrFileList串strJPGImagePath){
位图bmpDest;
使用(位图bmpOrig =新位图(strJPGImagePath)){
bmpDest =新位图(bmpOrig,新的大小(100,200));
}
bmpDest.Save(strJPGImagePath,jgpEn codeR,myEn coderParameters);
bmpDest.Dispose();
}
I am writing a code to resize JPG images in C#. My code takes around 6 seconds to resize 20 JPG images. I am wondering if there is any faster way of doing this in C#? Any suggestion to improve this is appreciated!
Here is my code now:
Bitmap bmpOrig, bmpDest, bmpOrigCopy;
foreach (string strJPGImagePath in strarrFileList)
{
bmpOrig = new Bitmap(strJPGImagePath);
bmpOrigCopy = new Bitmap(bmpOrig);
bmpOrig.Dispose();
File.Delete(strJPGImagePath);
bmpDest = new Bitmap(bmpOrigCopy, new Size(100, 200));
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
bmpOrigCopy.Dispose();
bmpDest.Dispose();
}
Thanks to @Guffa for his solution. I moved the dispose() out of foreach loop. The updated and fast code is:
Bitmap bmpDest = new Bitmap(1, 1);
foreach (string strJPGImagePath in strarrFileList)
{
using (Bitmap bmpOrig = new Bitmap(strJPGImagePath))
{
bmpDest = new Bitmap(bmpOrig, new Size(100, 200));
}
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
}
bmpDest.Dispose();
Instead of copying the bitmaps in two steps, make it one step. That way you reduce the memory usage quite a bit as you don't have two copies of the oringal image in memory at once.
foreach (string strJPGImagePath in strarrFileList) {
Bitmap bmpDest;
using(Bitmap bmpOrig = new Bitmap(strJPGImagePath)) {
bmpDest = new Bitmap(bmpOrig, new Size(100, 200));
}
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
bmpDest.Dispose();
}
这篇关于在C#中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!