您好,登錄后才能下訂單哦!
本篇內容主要講解“VB.NET實現圖像操作的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“VB.NET實現圖像操作的方法”吧!
VB.NET最為一款功能強大的.NET編程語言,其實用價值在開發領域是公認的。我們在這里將會為大家介紹一下有關VB.NET圖像操作的相關實現技巧,從另一角度去慢慢體會其功能應用的簡便及強大性。
慢速,這是以像素點操作為代表:
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
快速,以內存指針操作為代表,這是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
'得到***個像素的指針'數組操作()
Dim bytes As Integer =
bmpData.Stride * HeightDim rgbValues(bytes - 1) As Byte
Marshal.Copy(ptr, rgbValues, 0, bytes)
'將內存塊復制到數組,這是該方法的關鍵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)
'再將數組復制到內存塊'數組操作結束
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圖像操作的速度大約比數組加指針慢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
'得到***個像素的指針''指針操作 在這種模式下,比數組操作要慢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
''指針操作結束
Pic.UnlockBits(bmpData)
Return Pic
End Function
到此,相信大家對“VB.NET實現圖像操作的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。