调整位图图像的大小并使线条更粗
Resize bitmap image and make bolder lines
我尝试从原始图像创建缩略图,以便在我的 asp.net MVC 应用程序中使用。
使用下面的代码,我可以完成任务,但结果不够清楚。
我的问题是是否有更好的方法来做到这一点,是否有可能使结果像图像上的预期结果一样清晰。
这是我的代码:
var tracedPath = @"C:\Users\co\Desktop\traced.png";
var targetPath = @"C:\Users\co\Desktop\thumbnail.png";
Bitmap bmpSource = new Bitmap(tracedPath);
Bitmap bmpTarget = new Bitmap(224, 210);
var width = 85;
var height = 210;
var bmpResizedSource = new Bitmap(bmpSource, width, height);
using (Graphics grD = Graphics.FromImage(bmpTarget))
{
grD.DrawImage(bmpResizedSource, new RectangleF((224 - 75) / 2, 5, 75, 200), new RectangleF(0, 0, 85, 210), GraphicsUnit.Pixel);
}
bmpTarget.Save(targetPath);
Color pixel = Color.Transparent;
for (int x = 0; x < 224; x++)
{
for (int y = 0; y < 210; y++)
{
pixel = bmpTarget.GetPixel(x, y);
if (pixel.A > 0)
{
bmpTarget.SetPixel(x, y, Color.Black);
}
}
}
bmpTarget.Save(targetPath);
这是解释我想做什么的图片:
不确定这是否会对您的情况有所帮助,但由于我需要添加图片,因此必须将其作为答案发布...
一般来说,您问题的答案在很大程度上取决于图像内容。如果您总是要有黑白草图,这将有所帮助:
重新缩放图像,对其应用低半径高斯,并增加其对比度/降低其透明度,结果如下所示。您可以通过改变高斯滤波器的半径来改变线条粗细。请注意,高 gaus 会导致您的图像完全消失
*还要考虑是否不能使用路径而不是图像来显示这样的形状
这是一个主要来自
的函数
Bitmap Bolden(Bitmap bmp0)
{
float f = 2f;
Bitmap bmp = new Bitmap(bmp0.Width, bmp0.Height);
using (Bitmap bmp1 = new Bitmap(bmp0, new Size((int)( bmp0.Width * f),
(int)( bmp0.Height * f))))
{
float contrast = 1f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {contrast, 0, 0, 0, 0},
new float[] {0,contrast, 0, 0, 0},
new float[] {0, 0, contrast, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
attributes.SetGamma(7.5f, ColorAdjustType.Bitmap);
using (Graphics g = Graphics.FromImage(bmp))
g.DrawImage(bmp1, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp1.Width, bmp1.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
应用一次的结果:
您可以更频繁地应用它,在第 2 次迭代时使用对比度 > 1..
说明:我首先缩放原始图像,从而产生一点模糊(抗锯齿)。然后我通过应用大伽马使所有灰色像素变暗,最后恢复原始大小。这与 Denis post 的想法基本相同,但在工作 GDI+ 代码中..
我尝试从原始图像创建缩略图,以便在我的 asp.net MVC 应用程序中使用。
使用下面的代码,我可以完成任务,但结果不够清楚。
我的问题是是否有更好的方法来做到这一点,是否有可能使结果像图像上的预期结果一样清晰。
这是我的代码:
var tracedPath = @"C:\Users\co\Desktop\traced.png";
var targetPath = @"C:\Users\co\Desktop\thumbnail.png";
Bitmap bmpSource = new Bitmap(tracedPath);
Bitmap bmpTarget = new Bitmap(224, 210);
var width = 85;
var height = 210;
var bmpResizedSource = new Bitmap(bmpSource, width, height);
using (Graphics grD = Graphics.FromImage(bmpTarget))
{
grD.DrawImage(bmpResizedSource, new RectangleF((224 - 75) / 2, 5, 75, 200), new RectangleF(0, 0, 85, 210), GraphicsUnit.Pixel);
}
bmpTarget.Save(targetPath);
Color pixel = Color.Transparent;
for (int x = 0; x < 224; x++)
{
for (int y = 0; y < 210; y++)
{
pixel = bmpTarget.GetPixel(x, y);
if (pixel.A > 0)
{
bmpTarget.SetPixel(x, y, Color.Black);
}
}
}
bmpTarget.Save(targetPath);
这是解释我想做什么的图片:
不确定这是否会对您的情况有所帮助,但由于我需要添加图片,因此必须将其作为答案发布...
一般来说,您问题的答案在很大程度上取决于图像内容。如果您总是要有黑白草图,这将有所帮助:
重新缩放图像,对其应用低半径高斯,并增加其对比度/降低其透明度,结果如下所示。您可以通过改变高斯滤波器的半径来改变线条粗细。请注意,高 gaus 会导致您的图像完全消失
*还要考虑是否不能使用路径而不是图像来显示这样的形状
这是一个主要来自
Bitmap Bolden(Bitmap bmp0)
{
float f = 2f;
Bitmap bmp = new Bitmap(bmp0.Width, bmp0.Height);
using (Bitmap bmp1 = new Bitmap(bmp0, new Size((int)( bmp0.Width * f),
(int)( bmp0.Height * f))))
{
float contrast = 1f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {contrast, 0, 0, 0, 0},
new float[] {0,contrast, 0, 0, 0},
new float[] {0, 0, contrast, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
attributes.SetGamma(7.5f, ColorAdjustType.Bitmap);
using (Graphics g = Graphics.FromImage(bmp))
g.DrawImage(bmp1, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp1.Width, bmp1.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
应用一次的结果:
您可以更频繁地应用它,在第 2 次迭代时使用对比度 > 1..
说明:我首先缩放原始图像,从而产生一点模糊(抗锯齿)。然后我通过应用大伽马使所有灰色像素变暗,最后恢复原始大小。这与 Denis post 的想法基本相同,但在工作 GDI+ 代码中..