{
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;
}
}
if (openFileDialog1.ShowDialog() == DialogResult.OK) ////由對話框選取圖檔
{
myBitmap = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = myBitmap;
}
myBitmap.UnlockBits(byteArray);
return ImgData;
}
像素的R翻譯社 G翻譯社 B值做讀出和寫入的動作翻譯但如果對一張百萬像素的影像做影像
[C#] 載入影象檔一文中提到,C# 說話可使用
}
}
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++)
{
pictureBox2.Image = null;
}
public static Bitmap CreateBitmap(int[, ,] ImgData)
{
int Width = ImgData.GetLength(0);
int Height = ImgData.GetLength(1);
Bitmap myBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Bitmap myBitmap = new Bitmap( ImageFileName );
int gray = (int) ((double) ImgData[x, y, 0] * 0.299 + (double) ImgData[x, y, 1] * 0.587 + (double) ImgData[x, y, 2] * 0.114);
參考資料:
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";
讀入影像檔轉為像素資料陣列,並進行灰階(Grey Scale) 處置.
程式碼如下:
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, Width, Height),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
//Padding bytes的長度
int ByteOfSkip = byteArray.Stride - myBitmap.Width * 3;
BitmapData byteArray = myBitmap.LockBits( new Rectangle( 0 翻譯社 0 翻譯社 myBitmap.Width 翻譯社 myBitmap.Height ) 翻譯社 ImageLockMode.ReadWrite 翻譯社 PixelFormat.Format24bppRgb );
處置懲罰,則必需反複呼喚上述兩個函式上百萬次,這類方式對程式而言顯得太沒有用率翻譯
Basic Image Processing support in C#>
並以Bitmap類型的GetPixel(x翻譯社y)及SetPixel(x翻譯社y翻譯社color)兩個函數,對影象的每個
ImgData[x, y, 0] = gray;
ImgData[x, y翻譯社 1] = gray;
ImgData[x翻譯社 y翻譯社 2] = gray;
指標內容的體例,將影象像素資料一一讀入預定的像素陣列中並進行後續的影象處置,以加速速度。
範例程式:
private void button2_Click(object sender, EventArgs e) ///灰階影象處理
{
int[翻譯社 ,] ImgData = GetImgData(myBitmap);
GrayProcess(ImgData);
Bitmap processedBitmap = CreateBitmap(ImgData);
pictureBox2.Image = processedBitmap;
語法以下:
}
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程式碼須拔取。
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;
}
C#的Bitmap類型另供應一LockBits()函式可將影象的指定區域資料以Byte Array的型式寄存在記憶體空間中翻譯社。並傳回BitmapData類型的物件,此中BitmapData.Scan0則寄存像素Byte Array中第一個Byte的指標。 程式可以讀取
Bitmap myBitmap = new Bitmap(ImageFileName) 載入影象檔.
}
}
}
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;
}
}
本文引用自: http://mypaper.pchome.com.tw/middlehuang/post/1321779350有關各國語文翻譯公證的問題歡迎諮詢天成翻譯公司02-77260931