BitMap 如何秀出 Byte 陣列資料(C#)

来源:互联网 发布:如何自己开淘宝网店 编辑:程序博客网 时间:2024/05/17 02:39
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 

namespace BitMap_ShowData_CSharp 

public partial class Form1 : Form 

int mwidth; 
int mheight; 
ColorPalette pal; 
Bitmap img; 
BitmapData bitmapData; 
Rectangle rect; 
byte[] data; 

[DllImport("kernel32", EntryPoint = "RtlMoveMemory", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)] 
private static extern void CopyMemory(int Destination, int Source, int Length); 

public Form1() 

InitializeComponent(); 


private void button1_Click(object sender, EventArgs e) 

int[] ptr = new int[2]; 
int i; 
Random randObj = new Random() ; 

for (i = 0; i <= data.GetUpperBound(0); i++) 

data[i] = (byte)(randObj.Next(256)); 

bitmapData = img.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); 
ptr[0] = bitmapData.Scan0.ToInt32(); 
ptr[1] = VarPtr(data); 
CopyMemory(ptr[0], ptr[1], mwidth * mheight); 
img.UnlockBits(bitmapData); 
//img.RotateFlip(RotateFlipType.RotateNoneFlipX) 
pictureBox1.Image = img; 


private void Form1_Load(object sender, EventArgs e) 

int i; 
int Alpha; 

pictureBox1.BackColor = Color.Black; 
mwidth = 3324 & 0xFFFC; 
mheight = 2504; 
data = new byte[mwidth * mheight - 1 + 1]; 
img = new Bitmap(mwidth, mheight, PixelFormat.Format8bppIndexed); 
pal = img.Palette; 
Alpha = 255; 
for (i = 0; i <= 255; i++) 

pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); 

img.Palette = pal; 
rect = new Rectangle(0, 0, mwidth, mheight); 


private int VarPtr(object obj) 

int returnValue; 
GCHandle GC = GCHandle.Alloc(obj, GCHandleType.Pinned); 

returnValue = GC.AddrOfPinnedObject().ToInt32(); 
GC.Free(); 
return returnValue; 



}
原创粉丝点击