如何加载和优化位图
How Bitmap is loaded and optimized
我用Bitmap包生成了自定义验证码,如何让它生成的图片更大更清晰。这是怎么做到的?再次感谢您的帮助。
enter image description here
我搜索了一些答案,none 似乎有不错的结果
optimizing bitmap loading by using aSyncTask
试试下面的代码,将图片修改为JPEG格式,可以缩小图片大小,比较清晰。
static void ImgTestTwo()
{
string filename = @"E:\CoreWork\backone.jpg";
string targetname = @"E:\CoreWork\temp1.jpg";
Bitmap bitmap = new Bitmap(filename);
//Keep the ratio of the image unchanged, zoom the image
int width = 1000,
height = 1000;
if (bitmap.Width > bitmap.Height)
{
//Width is large, calculate height
height = Convert.ToInt32(width * (bitmap.Height * 1.0 / bitmap.Width));
}
else
{
//height is large, calculate width
width = Convert.ToInt32(height * (bitmap.Width * 1.0 / bitmap.Height));
}
Bitmap result = ResizeImage(bitmap, width, height);
filename = filename.Substring(0,filename.LastIndexOf('.'))+".jpg";
//Save the picture and specify the save format as Jpeg, which will take up less space
result.Save(targetname,ImageFormat.Jpeg);
result.Dispose();
bitmap.Dispose();
}
/// <summary>
/// Resize img
/// </summary>
/// <param name="bmp">original Bitmap </param>
/// <param name="newW">new width</param>
/// <param name="newH">new heights</param>
/// <returns>Process later pictures</returns>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// The quality of the interpolation algorithm
//g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch(Exception ex)
{
return null;
}
}
我用Bitmap包生成了自定义验证码,如何让它生成的图片更大更清晰。这是怎么做到的?再次感谢您的帮助。
enter image description here
我搜索了一些答案,none 似乎有不错的结果
optimizing bitmap loading by using aSyncTask
试试下面的代码,将图片修改为JPEG格式,可以缩小图片大小,比较清晰。
static void ImgTestTwo()
{
string filename = @"E:\CoreWork\backone.jpg";
string targetname = @"E:\CoreWork\temp1.jpg";
Bitmap bitmap = new Bitmap(filename);
//Keep the ratio of the image unchanged, zoom the image
int width = 1000,
height = 1000;
if (bitmap.Width > bitmap.Height)
{
//Width is large, calculate height
height = Convert.ToInt32(width * (bitmap.Height * 1.0 / bitmap.Width));
}
else
{
//height is large, calculate width
width = Convert.ToInt32(height * (bitmap.Width * 1.0 / bitmap.Height));
}
Bitmap result = ResizeImage(bitmap, width, height);
filename = filename.Substring(0,filename.LastIndexOf('.'))+".jpg";
//Save the picture and specify the save format as Jpeg, which will take up less space
result.Save(targetname,ImageFormat.Jpeg);
result.Dispose();
bitmap.Dispose();
}
/// <summary>
/// Resize img
/// </summary>
/// <param name="bmp">original Bitmap </param>
/// <param name="newW">new width</param>
/// <param name="newH">new heights</param>
/// <returns>Process later pictures</returns>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// The quality of the interpolation algorithm
//g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch(Exception ex)
{
return null;
}
}