private void translation_Click(object sender, EventArgs e) { if (curBitmap != null) { translation traForm = new translation(); #region 简单方法--像素法 //if (traForm.ShowDialog() == DialogResult.OK) //{ // // handleBitmap = (Bitmap) curBitmap.Clone(); // Color curColor; // int ret; // for (int i = 0; i < handleBitmap.Width; i++) // { // for (int j = 0; j < handleBitmap.Height; j++) // { // curColor = handleBitmap.GetPixel(i, j); // //ret = (int) (curColor.R*0.299 + curColor.G*0.587 + curColor.B*0.114); // ret = Convert.ToInt32(tb_FillColor.Text); // handleBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret)); // } // } //} //Invalidate(); //return; #endregion #region 原始方法 if (traForm.ShowDialog() == DialogResult.OK) { // offsetOPbyMemory(traForm); offsetOPbyPointer(traForm); } Invalidate(); #endregion tb_timer.Text = timercount.Duration.ToString("####.##") + " 毫秒"; } }
private void translation_Click(object sender, EventArgs e) { if (curBitmap != null) { translation traForm = new translation(); if (traForm.ShowDialog() == DialogResult.OK) { Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = curBitmap.Width * curBitmap.Height; byte[] grayValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); int x = Convert.ToInt32(traForm.GetXOffset); int y = Convert.ToInt32(traForm.GetYOffset); byte[] tempArray = new byte[bytes]; //Array.Clear(tempArray, 0, bytes); for (int i = 0; i < bytes; i++) { tempArray[i] = 255; } for (int i = 0; i < curBitmap.Height; i++) { if ((i + y) < curBitmap.Height && (i + y) > 0) { for (int j = 0; j < curBitmap.Width; j++) { if ((j + x) < curBitmap.Width && (j + x) > 0) { tempArray[(j + x) + (i + y) * curBitmap.Width] = grayValues[j + i * curBitmap.Width]; } } } } grayValues = (byte[])tempArray.Clone(); System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes); curBitmap.UnlockBits(bmpData); } Invalidate(); } }