3
A
回答
6
您可以尝试获取第一张图像d ata(有图像),并将数据绘制成新图像。试试这个方法。希望它能帮助你。
private static Bitmap ImageTrim(Bitmap img)
{
//get image data
BitmapData bd= img.LockBits(new Rectangle(Point.Empty, img.Size),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int[] rgbValues = new int[img.Height * img.Width];
Marshal.Copy(bd.Scan0, rgbValues, 0, rgbValues.Length);
img.UnlockBits(bd);
#region determine bounds
int left = bd.Width;
int top = bd.Height;
int right = 0;
int bottom = 0;
//determine top
for (int i = 0; i < rgbValues.Length; i++)
{
int color = rgbValues[i] & 0xffffff;
if (color != 0xffffff)
{
int r = i/bd.Width;
int c = i % bd.Width;
if (left > c)
{
left = c;
}
if (right < c)
{
right = c;
}
bottom = r;
top = r;
break;
}
}
//determine bottom
for (int i = rgbValues.Length - 1; i >= 0; i--)
{
int color = rgbValues[i] & 0xffffff;
if (color != 0xffffff)
{
int r = i/bd.Width;
int c = i % bd.Width;
if (left > c)
{
left = c;
}
if (right < c)
{
right = c;
}
bottom = r;
break;
}
}
if (bottom > top)
{
for (int r = top + 1; r < bottom; r++)
{
//determine left
for (int c = 0; c < left; c++)
{
int color = rgbValues[r * bd.Width + c] & 0xffffff;
if (color != 0xffffff)
{
if (left > c)
{
left = c;
break;
}
}
}
//determine right
for (int c = bd.Width - 1; c > right; c--)
{
int color = rgbValues[r * bd.Width + c] & 0xffffff;
if (color != 0xffffff)
{
if (right < c)
{
right = c;
break;
}
}
}
}
}
int width = right - left + 1;
int height = bottom - top + 1;
#endregion
//copy image data
int[] imgData = new int[width * height];
for (int r = top; r <= bottom; r++)
{
Array.Copy(rgbValues, r * bd.Width + left, imgData, (r - top) * width, width);
}
//create new image
Bitmap newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData nbd
= newImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(imgData, 0, nbd.Scan0, imgData.Length);
newImage.UnlockBits(nbd);
return newImage;
}
2
如果你的图像只有2种颜色(白色和黑色),你可以遍历你的图像,找到左上角的像素集和右下角的像素集,然后你可以裁剪它: (伪代码,取决于你用它来获得图像的像素)
int minX = int.MaxValue, maxX = 0, minY = int.MaxValue, maxY = 0;
for (x = 0; x < image.Width, x++)
{
for (y = 0; y < image.Height; y++)
{
if (image[x, y] == 1)
{
if (x < minX) minX = x;
else if (x > maxX) maxX = x;
if (y < minY) minY = y;
else if (y > maxY) maxY = y;
}
}
}
那么你就必须,可以让你裁剪图像
我敢肯定,这可以优化坐标但这是一般的想法
相关问题
- 1. 从图像中裁剪空白
- 2. 裁剪图像后的空白窗口
- 3. 裁剪从图像在C#中的空白
- 4. C# - 裁剪透明/空白
- 5. c中的裁剪图像#
- 6. 图像裁剪c#
- 7. Ruby Rmagick:裁剪/“修剪”图像的过多(白色)空间
- 8. 裁剪多余的空白
- 9. 图像magick图像裁剪增加了空白
- 10. WinRT中的裁剪/裁剪图像
- 11. PHP中的图像裁剪产生空白结果
- 12. 使用jQuery自动裁剪图像空白空间
- 13. 裁剪图像C#Distorion
- 14. 裁剪图像objective-c
- 15. 使用c裁剪图像#
- 16. c#自动裁剪图像
- 17. 在C#中裁剪和编辑图像
- 18. 使用c在asp.net中裁剪图像#
- 19. 在android中裁剪图像
- 20. 在Java中裁剪图像
- 21. 在Matlab中裁剪图像
- 22. 在HTML5中裁剪图像
- 23. 在Richfaces中裁剪图像
- 24. 在matlab中裁剪图像
- 25. 在android中裁剪图像
- 26. 在CSS中裁剪图像
- 27. 在opencv中裁剪图像
- 28. 在PHP中裁剪图像
- 29. 在Java中裁剪图像
- 30. 在wordpress中裁剪图像
衡量你可以从两边走多深,让你有一个不间断的白色像素线。 – spender
检查此链接有关图像大小调整和裁剪。它可能有所帮助:http://jasonjano.wordpress.com/2010/02/13/image-resizing-and-cropping-in-c/ –
找到你的顶部,底部,左侧和右侧的黑点的协调和作物矩形像http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c – Alex