private Bitmap ConvertToPic(byte[] picByte, int width, int height) { byte[,] b = new byte[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { b[i, j] = picByte[i * width + j]; //b.SetPixel(i, j, c); } } ByteToImage byteToImage = new ByteToImage(); return(byteToImage.FromGray(b)); // Origin_Pic.Image = byteToImage.FromGray(ImageBuffer); //return b; }
private Bitmap ConvertToPic(byte[] picByte, int width, int height) { byte[,] b = new byte[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { b[i, j] = picByte[i * width + j]; //b.SetPixel(i, j, c); } } ByteToImage byteToImage = new ByteToImage(); return byteToImage.FromGray(b); // Origin_Pic.Image = byteToImage.FromGray(ImageBuffer); //return b; }
//从大图上截取模板大小的图片并保存到本地 private void CutBmpbutton1_Click(object sender, EventArgs e) { int i; int j; try { if (CutBmptextBox1.Text != "") //当截图名不为空时 { Bitmap temBmp = CutImage(bmp, templetFlag.templetPoint.X, templetFlag.templetPoint.Y, templetFlag.templetSize.Width, templetFlag.templetSize.Height); ByteToImage bytetoimage = new ByteToImage(); byte[,] temData = new byte[temBmp.Height,temBmp.Width]; for (i = 0; i < temBmp.Height; i++) { for (j = 0; j < temBmp.Width; j++) { temData[i, j] = temBmp.GetPixel(j, i).R; } } bytetoimage.FromGray(temData).Save(savePath + "\\NumberBmp\\" + CurrentTempletName + "\\" + CutBmptextBox1.Text + ".bmp", ImageFormat.Bmp);//24位 FeatherdataGrid.Rows.Clear(); DirectoryInfo TheFolder = new DirectoryInfo(savePath + "\\NumberBmp" + "\\" + CurrentTempletName); FileInfo[] NextFile = TheFolder.GetFiles(); //遍历文件夹内文件 将存在的图片名添加到表格内 int count = 0; for (i = 0; i < NextFile.Length; i++) { if (NextFile[i].Name != "templet.txt") { count++; FeatherdataGrid.Rows.Add(count.ToString(), StringOperateHelp.LeftOf(NextFile[i].Name, '.'), null); } } } } catch { } NextStep.Enabled = true; }
//截图函数 public static Bitmap CutImage(Bitmap b, int StartX, int StartY, int iWidth, int iHeight) { if (b == null) { return null; } int w = b.Width; int h = b.Height; byte[,] data = new byte[iHeight,iWidth]; if (StartX >= w || StartY >= h) { return null; } if (StartX + iWidth > w) { iWidth = w - StartX; } if (StartY + iHeight > h) { iHeight = h - StartY; } ByteToImage bytetoimage=new ByteToImage(); try { Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut); g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel); g.Dispose(); for (int i = 0; i < iHeight; i++) for (int j = 0; j < iWidth; j++) { data[i, j] =(byte)bmpOut.GetPixel(j, i).R; } return bytetoimage.FromGray(data); } catch { return null; } }
//将截图所得图片拼接成大图 private void PasteNumber(List<Bitmap> tmpList) { try { byte[,] bitData; bitData = new byte[tmpList.First().Height, tmpList.First().Width * tmpList.Count]; //新建个大小为所有图片拼接起来大小的数组 int flag = 0; foreach (Bitmap tmp in tmpList) //将图像像素点填充到数组里 { for (int i = 0; i < tmp.Height; i++) { for (int j = 0; j < tmp.Width; j++) { bitData[i, j + tmp.Width * flag] = tmp.GetPixel(j, i).R; } } flag++; } ByteToImage bytetoimage = new ByteToImage(); //将数组转化为图片 Bitmap bitmap = bytetoimage.FromGray(bitData); picturebox.Size = new System.Drawing.Size(tmpList.First().Width * tmpList.Count, tmpList.First().Height); picturebox.Image = bitmap; RePlacebitmap(bitmap); //将图片放在panel中心位置 } catch { MessageBox.Show("图片有误"); } }