本篇內(nèi)容主要講解“VB.NET實(shí)現(xiàn)圖像操作的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“VB.NET實(shí)現(xiàn)圖像操作的方法”吧!
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括崇禮網(wǎng)站建設(shè)、崇禮網(wǎng)站制作、崇禮網(wǎng)頁制作以及崇禮網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,崇禮網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到崇禮省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
VB.NET最為一款功能強(qiáng)大的.NET編程語言,其實(shí)用價(jià)值在開發(fā)領(lǐng)域是公認(rèn)的。我們在這里將會為大家介紹一下有關(guān)VB.NET圖像操作的相關(guān)實(shí)現(xiàn)技巧,從另一角度去慢慢體會其功能應(yīng)用的簡便及強(qiáng)大性。
慢速,這是以像素點(diǎn)操作為代表:
Public Function fan_slow(ByVal
inputImage As Image) As Image
Dim pic As Bitmap =
New Bitmap(inputImage)
Dim i As Integer, j As Integer
Dim R As Integer, G As
Integer, B As Integer
Dim Width As Integer,
Height As Integer
Width = Pic.Width :
Height = Pic.Height
Dim myColor As Color
For i = 0 To Height - 1
For j = 0 To Width - 1
R = 255-pic.GetPixel(i, j).R
G = 255-pic.GetPixel(i, j).G
B = 255-pic.GetPixel(i, j).B
myColor = Color.FromArgb(R, G, B)
pic.SetPixel(i, j, myColor)
Next
Next
Return pic
End Function
快速,以內(nèi)存指針操作為代表,這是VB.NET圖像操作中最快的方法
Public Function fan_fast(ByVal
inputImage As Image) As ImageDim R As Byte, G As Byte, B As
Byte, Col As ByteDim Width As Integer, Height
As IntegerDim Pic As Bitmap = New
Bitmap(inputImage)Width = Pic.Width :
Height = Pic.HeightDim rect As New Rectangle(0, 0,
Width, Height)Dim bmpData As BitmapData =
Pic.LockBits(rect, ImageLockMode.
ReadWrite, Pic.PixelFormat)Dim ptr As IntPtr = bmpData.Scan0
'得到***個(gè)像素的指針'數(shù)組操作()
Dim bytes As Integer =
bmpData.Stride * HeightDim rgbValues(bytes - 1) As Byte
Marshal.Copy(ptr, rgbValues, 0, bytes)
'將內(nèi)存塊復(fù)制到數(shù)組,這是該方法的關(guān)鍵For k As Integer = 0 To
rgbValues.Length - 4 Step 4B = CByte(255 - rgbValues(k))
G = CByte(255 - rgbValues(k + 1))
R = CByte(255 - rgbValues(k + 2))
rgbValues(k) = B
rgbValues(k + 1) = G
rgbValues(k + 2) = R
Next
Marshal.Copy(rgbValues, 0, ptr, bytes)
'再將數(shù)組復(fù)制到內(nèi)存塊'數(shù)組操作結(jié)束
Pic.UnlockBits(bmpData)
Return Pic
End Function
還有一種以C#中的非安全代碼 指針操作
public Bitmap fan_fast2(Bitmap b)
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits
(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * 4;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++)
{
p[2] ^= 0xFF;
p[1] ^= 0xFF;
p[0] ^= 0xFF;
p += 4;
}
p += offset;
}
b.UnlockBits(data);
return b;
}
}
如果要改造成vb.net,就是這樣,VB.NET圖像操作的速度大約比數(shù)組加指針慢2-3倍
Public Function fan_fast2(ByVal
inputImage As Image) As ImageDim R As Byte, G As Byte,
B As Byte, Col As ByteDim Width As Integer,
Height As IntegerDim Pic As Bitmap =
New Bitmap(inputImage)Width = Pic.Width : Height =
Pic.HeightDim rect As New Rectangle
(0, 0, Width, Height)Dim bmpData As BitmapData =
Pic.LockBits(rect, ImageLockMode.
ReadWrite, Pic.PixelFormat)Dim ptr As IntPtr = bmpData.Scan0
'得到***個(gè)像素的指針''指針操作 在這種模式下,比數(shù)組操作要慢2-3倍
Dim offset As Integer = bmpData.
Stride - bmpData.Width * 4For j As Integer = 0 To Height - 1
For i As Integer = 0 To Width - 1
B = CByte(255 - Marshal.ReadByte(ptr))
G = CByte(255 - Marshal.ReadByte(ptr, 1))
R = CByte(255 - Marshal.ReadByte(ptr, 2))
Marshal.WriteByte(ptr, 0, B)
Marshal.WriteByte(ptr, 1, G)
Marshal.WriteByte(ptr, 2, R)
ptr = CType(ptr.ToInt32 + 4, IntPtr)
Next
ptr = CType(ptr.ToInt32 +
offset, IntPtr)Next
''指針操作結(jié)束
Pic.UnlockBits(bmpData)
Return Pic
End Function
到此,相信大家對“VB.NET實(shí)現(xiàn)圖像操作的方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!