private void GrayProcess(int[, ,] ImgData)
{
int Width = ImgData.GetLength(0);
int Height = ImgData.GetLength(1);
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
{
byte* imgPtr = (byte*)(byteArray.Scan0);
for (int y = 0; y < byteArray.Height; y++)
{
for (int x = 0; x < byteArray.Width; x++)
{
ImgData[x, y, 2] = (int) *(imgPtr);
ImgData[x翻譯社 y, 1] = (int) *(imgPtr + 1);
ImgData[x, y, 0] = (int) *(imgPtr + 2);
imgPtr += 3;
}
imgPtr += ByteOfSkip;
}
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap myBitmap = null;
private void button1_Click(object sender翻譯社 EventArgs e) ////載入圖檔
{
this.openFileDialog1.Filter = "所有檔案|*.*|BMP File| *.bmp|JPEG File|*.jpg| GIF File|*.gif";
典範程式:
並以Bitmap類型的GetPixel(x翻譯社y)及SetPixel(x,y,color)兩個函數,對影像的每個
ImgData[x, y, 0] = gray;
ImgData[x, y, 1] = gray;
ImgData[x翻譯社 y, 2] = gray;
public static Bitmap CreateBitmap(int[翻譯社 ,] ImgData)
{
int Width = ImgData.GetLength(0);
int Height = ImgData.GetLength(1);
Bitmap myBitmap = new Bitmap(Width, Height翻譯社 PixelFormat.Format24bppRgb);
pictureBox2.Image = null;
}
處理,則必需重複呼喚上述兩個函式上百萬次,這種體式格局對程式而言顯得太沒有用率翻譯
}
}
unsafe
{ // 指標掏出影象資料
byte* imgPtr = (byte*) byteArray.Scan0;
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
*imgPtr = (byte)ImgData[x翻譯社 y, 2]; //B
*(imgPtr+1) = (byte)ImgData[x, y, 1]; //G
*(imgPtr+2) = (byte)ImgData[x翻譯社 y, 0]; //R
imgPtr += 3;
}
imgPtr += ByteOfSkip; // 跳過Padding bytes
}
}
myBitmap.UnlockBits(byteArray);
return myBitmap;
}
}
private int[, ,] GetImgData(Bitmap myBitmap)
{
int[,翻譯社] ImgData = new int[myBitmap.Width, myBitmap.Height, 3];
BitmapData byteArray = myBitmap.LockBits(new Rectangle(0, 0翻譯社 myBitmap.Width, myBitmap.Height)翻譯社 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int ByteOfSkip = byteArray.Stride - byteArray.Width * 3;
unsafe //專案->屬性->建置->允許Unsafe程式碼須選取。
程式碼以下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
BitmapData byteArray = myBitmap.LockBits( new Rectangle( 0 翻譯社 0 , myBitmap.Width , myBitmap.Height ) , ImageLockMode.ReadWrite 翻譯社 PixelFormat.Format24bppRgb );
Basic Image Processing support in C#>
Bitmap myBitmap = new Bitmap(ImageFileName) 載入影象檔.
指標內容的方式,將影象像素資料逐一讀入預定的像素陣列中並進行後續的影象處理,以加速速度。
private void button2_Click(object sender, EventArgs e) ///灰階影象處理 BitmapData byteArray = myBitmap.LockBits(new Rectangle(0, 0, Width, Height)翻譯社 } 語法以下:
{
int[翻譯社 ,] ImgData = GetImgData(myBitmap);
GrayProcess(ImgData);
Bitmap processedBitmap = CreateBitmap(ImgData);
pictureBox2.Image = processedBitmap;
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
//Padding bytes的長度
int ByteOfSkip = byteArray.Stride - myBitmap.Width * 3;
}
}
讀入影象檔轉為像素資料陣列,並進行灰階(Grey Scale) 處理.
if (openFileDialog1.ShowDialog() == DialogResult.OK) ////由對話框選取圖檔
{
myBitmap = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = myBitmap;
}
myBitmap.UnlockBits(byteArray);
return ImgData;
}
int gray = (int) ((double) ImgData[x翻譯社 y, 0] * 0.299 + (double) ImgData[x翻譯社 y, 1] * 0.587 + (double) ImgData[x, y翻譯社 2] * 0.114);
參考資料:
[C#] 載入影像檔一文中提到,C# 說話可以使用
C#的Bitmap類型另提供一LockBits()函式可將影象的指定區域資料以Byte Array的型式存放在記憶體空間中,。並傳回BitmapData類型的物件,個中BitmapData.Scan0則寄存像素Byte Array中第一個Byte的指標。 程式可以讀取
unsafe //進行指標處理所須宣佈。專案->屬性->建置->容許Unsafe程式碼須拔取。
{
byte* imgPtr = (byte*)(byteArray.Scan0);
for (int y = 0; y < byteArray.Height; y++)
{
for (int x = 0; x < byteArray.Width; x++)
{
ImgData[x, y, 2] = (int) *(imgPtr);
ImgData[x, y翻譯社 1] = (int) *(imgPtr + 1);
ImgData[x, y, 0] = (int) *(imgPtr + 2);
imgPtr += 3;
}
imgPtr += ByteOfSkip;
}
}
Bitmap myBitmap = new Bitmap( ImageFileName );
像素的R, G翻譯社 B值做讀出和寫入的動作翻譯但若是對一張百萬像素的影像做影象
來自: http://mypaper.pchome.com.tw/middlehuang/post/1321779350有關各國語文翻譯公證的問題歡迎諮詢天成翻譯公司02-77260931