看板Programming
标 题Re: 聊聊VB的绘图效能
发信站梦之大地 (Sat Jul 29 01:51:56 2006)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.nsysu!news.mksh.phc!news.ccns.n
感谢 devil 和 StubbornLin 的指导,
以下是我在 MSDN 找到的资料,把它 post 上来供大家参考
'以下范例是将 PictureBox1显示的图快速扫描 and 将 RGB 中的 R 全部改为255
Dim myBitmap as Bitmap
'把要扫的图load进来
myBitmap = Me.PictureBox1.Image
'利用 lockbits 的方法将 myBitmap 的那内容值读出来
'lock the bitmap's bits
Dim Rec As New Rectangle(0, 0, myBitmap.Width, myBitmap.Height)
Dim Bmd As System.Drawing.Imaging.BitmapData =
myBitmap.LockBits(Rec, Imaging.ImageLockMode.ReadOnly,
myBitmap.PixelFormat)
'上面三行要串在一起
'抓取BMD first line 的记忆体位置
Dim Ptr As IntPtr = Bmd.Scan0
'宣告放图档 RGB 颜色值的 RGBArray 大小为 myBitmap的长*宽*3
'影像大小为M by N 的话,记忆体需求量,24bit影像为 M*N*3
'3是因为RGB各要一份,所以 32bit 影像为 M*N*4
Dim bytes As Integer = myBitmap.Width * myBitmap.Height * 3
Dim RGBArray(bytes - 1) As Byte
'Copy the RGB value into RGBArray
System.Runtime.InteropServices.Marshal.Copy(Ptr, RGBArray, 0, bytes)
'!!!! 这里是示范,把所有R的颜色改为255
For Counter As Integer = 2 To RGBArray.Length - 1 Step 3
RGBArray(Counter) = 255
Next
'Copy the RGB value back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(RGBArray, 0, Ptr, bytes)
'unlock the bits,记忆体锁起来就要打开
myBitmap.UnlockBits(Bmd)
'把图贴回去,这里是因为我在PictureBox 物件载入图形後即先扫图的内容
'所以 sub 本身没有 ByVal e As PaintEventArg
'故而得宣告一个来用
Dim p As New PaintEventArgs(Graphics.FromImage(PictureBox1.Image), Rec)
p.Graphics.DrawImage(myBitmap, 0, 0) '把图贴回 and 左上角的位置为(0,0)
--
菩提本无树 明镜亦非台
本来无一物 何处惹尘埃
~~~~禅宗六祖慧能
--
◢◣ ︵︵ █▔◣ █▔█ █▔▔ █▔█ █▆▉ █ █▔█ █◣█ █▔●
◢◤█◣◢◣ ︵︵ █ █ █▁◤ █▁▁ █▁█ ▉▉▉ █ █▁█ █◥█ █ █
梦之大地 逼逼ㄟ四 █▁◤ █ █ █▁▁ █ █ ▉▉▉ █▁ █ █ █ █ █▁◤
※ Origin: <bbs.ccns.ncku.edu.tw> ◆ From: 140.116.82.85