c# 图片自动去黑边思路
1、如果某一行(列)的所有像素点的颜色相差不大,则判断为黑边/白边
2、按照1方法依次去除上、下、左、右黑边/白边
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | public Bitmap CropBitmap(Bitmap originalBitmap, int top, int bottom, int left, int right) { // 计算新的图像大小 int newWidth = originalBitmap.Width - left - right; int newHeight = originalBitmap.Height - top - bottom; // 创建新的 Bitmap 对象 Bitmap croppedBitmap = new Bitmap(newWidth, newHeight); // 创建一个 Graphics 对象,用于绘制新的图像 Graphics g = Graphics.FromImage(croppedBitmap); // 绘制原始图像的一部分到新的图像中 g.DrawImage(originalBitmap, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(left, top, newWidth, newHeight), GraphicsUnit.Pixel); // 释放 Graphics 对象 g.Dispose(); // 返回裁剪后的图像 return croppedBitmap; } //去除上黑边 private Bitmap RemoveUpBlackEdge(Bitmap image) { int height = image.Height; int width = image.Width; Color firstPixel = image.GetPixel(0, 0); // 获取左上角第一个像素的颜色 int diffLine = 0; // 顶部黑边行数 for ( int y = 0; y < height; y++) // 从第一行开始比较 { bool darkLine = true ; for ( int x = 0; x < width; x++) { Color pixel = image.GetPixel(x, y); int deltaR = Math.Abs(pixel.R - firstPixel.R); int deltaG = Math.Abs(pixel.G - firstPixel.G); int deltaB = Math.Abs(pixel.B - firstPixel.B); int deltaTotal = deltaR + deltaG + deltaB; if (deltaTotal > 500 || deltaTotal < -500) { darkLine = false ; break ; } } diffLine++; if (!darkLine) // 如果这一行和第一行不同,记录下这一行的行数 { break ; } } return CropBitmap(image, diffLine, 0, 0, 0); } //去除下黑边 private Bitmap RemoveDownBlackEdge(Bitmap image) { int height = image.Height; int width = image.Width; Color firstPixel = image.GetPixel(0, height-1); // 获取左下角第一个像素的颜色 int diffLine = 0; // 底部黑边行数 for ( int y = height - 1; y > 1; y--) // 从最后一行开始比较 { bool darkLine = true ; for ( int x = 0; x < width; x++) { Color pixel = image.GetPixel(x, y); int deltaR = Math.Abs(pixel.R - firstPixel.R); int deltaG = Math.Abs(pixel.G - firstPixel.G); int deltaB = Math.Abs(pixel.B - firstPixel.B); int deltaTotal = deltaR + deltaG + deltaB; if (deltaTotal > 500 || deltaTotal < -500) { darkLine = false ; break ; } } diffLine++; if (!darkLine) // 如果这一行和第一行不同,记录下这一行的行数 { break ; } } if (diffLine == 0) // 如果所有行都相同 { return image; } else // 如果有不同的行 { return CropBitmap(image, 0, diffLine, 0, 0); } } private Bitmap RemoveLeftBlackEdge(Bitmap image) { int height = image.Height; int width = image.Width; Color firstPixel = image.GetPixel(0, 0); // 获取第一个像素的颜色 int diffLine = 0; // 顶部黑边行数 for ( int x = 0; x < width; x++) // 从第一行开始比较 { bool darkLine = true ; for ( int y = 0; y < height; y++) { Color pixel = image.GetPixel(x, y); //MessageBox.Show(pixel.ToArgb().ToString()); int deltaR = Math.Abs(pixel.R - firstPixel.R); int deltaG = Math.Abs(pixel.G - firstPixel.G); int deltaB = Math.Abs(pixel.B - firstPixel.B); int deltaTotal = deltaR + deltaG + deltaB; if (deltaTotal > 500 || deltaTotal < -500) { darkLine = false ; break ; } } diffLine++; if (!darkLine) // 如果这一行和第一行不同,记录下这一行的行数 { break ; } } if (diffLine == 0) // 如果所有行都相同 { return image; } else // 如果有不同的行 { return CropBitmap(image, 0, 0, diffLine, 0); } } // 去除右边黑边 private Bitmap RemoveRightBlackEdge(Bitmap image) { int height = image.Height; int width = image.Width; Color firstPixel = image.GetPixel(width - 1, 0); // 获取右上角第一个像素的颜色 int diffLine = 0; // 右边黑边列数 for ( int x = width - 1; x > 0; x--) // 从右边第一列开始比较 { bool darkLine = true ; for ( int y = 0; y < height; y++) { Color pixel = image.GetPixel(x, y); //MessageBox.Show(pixel.ToArgb().ToString()); int deltaR = Math.Abs(pixel.R - firstPixel.R); int deltaG = Math.Abs(pixel.G - firstPixel.G); int deltaB = Math.Abs(pixel.B - firstPixel.B); int deltaTotal = deltaR + deltaG + deltaB; if (deltaTotal > 500 || deltaTotal < -500) { //MessageBox.Show(pixel.ToArgb().ToString() + firstPixel.ToArgb().ToString()); darkLine = false ; break ; } } diffLine++; if (!darkLine) // 如果这一列和第一列不同,记录下这一列的序号 { break ; } } if (diffLine == 0) // 如果所有行都相同 { return image; } else // 如果有不同的行 { return CropBitmap(image, 0, 0, 0, diffLine); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架