删除位图c#上的alpha通道(仅对纯色图像进行切片)
我想得到两个图像之间的差异 我使用以下源代码:删除位图c#上的alpha通道(仅对纯色图像进行切片),c#,image-processing,bitmap,C#,Image Processing,Bitmap,我想得到两个图像之间的差异 我使用以下源代码: public Bitmap GetDifference(Bitmap bm1, Bitmap bm2) { if (bm1.Size != bm2.Size) throw new ArgumentException("exception"); var resultImage = new Bitmap(bm1.Width, bm1.Height);
public Bitmap GetDifference(Bitmap bm1, Bitmap bm2)
{
if (bm1.Size != bm2.Size)
throw new ArgumentException("exception");
var resultImage = new Bitmap(bm1.Width, bm1.Height);
using (Graphics g = Graphics.FromImage(resultImage))
g.Clear(Color.Transparent);
for (int w = 0; w < bm1.Width; w++)
{
for (int h = 0; h < bm1.Height; h++)
{
var bm2Color = bm2.GetPixel(w, h);
if (IsColorsDifferent(bm1.GetPixel(w, h), bm2Color))
{
resultImage.SetPixel(w, h, bm2Color);
}
}
}
return resultImage;
}
bool IsColorsDifferent(Color c1, Color c2)
{
return c1 != c2;
}
公共位图GetDifference(位图bm1、位图bm2)
{
如果(bm1.Size!=bm2.Size)
抛出新的ArgumentException(“异常”);
var resultImage=新位图(bm1.Width,bm1.Height);
使用(Graphics g=Graphics.FromImage(resultImage))
g、 透明(彩色、透明);
对于(int w=0;w谢谢 请看我在下面写的课程:
class ProcessingImage
{
public Bitmap GetDifference(Bitmap bm1, Bitmap bm2)
{
if (bm1.Size != bm2.Size)
throw new ArgumentException("Images must have one size");
var resultImage = new Bitmap(bm1.Width, bm1.Height);
using (Graphics g = Graphics.FromImage(resultImage))
g.Clear(Color.Transparent);
int min_height = int.MaxValue;
int min_width = int.MaxValue;
int max_height = -1;
int max_width = -1;
for (int w = 0; w < bm1.Width; w++)
for (int h = 0; h < bm1.Height; h++)
{
var bm2Color = bm2.GetPixel(w, h);
if (IsColorsDifferent(bm1.GetPixel(w, h), bm2Color))
{
resultImage.SetPixel(w, h, bm2Color);
if (h < min_height) min_height = h;
if (w < min_width) min_width = w;
if (h > max_height) max_height = h;
if (w > max_width) max_width = w;
}
}
max_height = max_height + 1; //Needed for get valid max height point, otherwise we lost one pixel.
max_width = max_width + 1; //Needed for get valid max width point, otherwise we lost one pixel.
// Calculate original size for image without alpha chanel.
int size_h = max_height - min_height;
int size_w = max_width - min_width;
var resizeImage = new Bitmap(size_w, size_h); // Creaete bitmap with new size.
for (int w = 0; w < resultImage.Width; w++)
for (int h = 0; h < resultImage.Height; h++)
{
var color = resultImage.GetPixel(w, h);
if (color.A != 0)
{
// Move each pixels at a distance calculate before.
int x = w - min_width;
int y = h - min_height;
resizeImage.SetPixel(x, y, color);
}
}
return resizeImage;
}
bool IsColorsDifferent(Color c1, Color c2)
{
return c1 != c2;
}
}
类处理图像
{
公共位图GetDifference(位图bm1、位图bm2)
{
如果(bm1.Size!=bm2.Size)
抛出新的ArgumentException(“图像必须有一个大小”);
var resultImage=新位图(bm1.Width,bm1.Height);
使用(Graphics g=Graphics.FromImage(resultImage))
g、 透明(彩色、透明);
int min_height=int.MaxValue;
int min_width=int.MaxValue;
int max_height=-1;
int max_width=-1;
对于(int w=0;w最大高度)最大高度=h;
如果(w>最大宽度)最大宽度=w;
}
}
max\u height=max\u height+1;//需要获取有效的max height点,否则会丢失一个像素。
max\u width=max\u width+1;//需要获取有效的最大宽度点,否则会丢失一个像素。
//计算没有alpha通道的图像的原始大小。
int size_h=最大高度-最小高度;
int size_w=最大宽度-最小宽度;
var resizeImage=新位图(大小为w,大小为h);//使用新大小创建位图。
for(int w=0;w
我认为,这个类可以修改以优化,但现在它工作得很好。如果您需要返回具有原始大小且没有alpha通道的图像-这是许多解决方案之一。当遇到alpha通道>0的像素时,您需要扫描图像并跟踪边界(假设您有矩形截面,这很容易)。还要注意的是
GetPixel
和SetPixel
像狗一样慢。非常慢的狗。如果他们为您工作,并且您知道您的输入图像将始终非常小,那么很好。如果图像的大小未知,我不认为深入研究不安全的上下文并通过锁位获得指向图像缓冲区的指针是过早的优化。谢谢,是的,我知道。这是将用于创建一个包,而不是在运行时使用(只有一次性计算)哦,另一个图像处理技巧;按行扫描图像,而不是按列扫描图像。这里的假设是图像数据按行存储在数组中(这几乎总是正确的)。数据的局部性在这里很重要。您的代码可能不会导致CPU缓存当前充满相邻数据。您将在每次迭代中刷新和填充缓存,这意味着从主内存获取数据的时间更长。如果你按行循环,它会。@EdS。你能把你的提示写下来作为答案吗?它可以对其他人有用。谢谢:请看上面关于执行速度的评论。