VB.NET实现图像操作的方法
本篇内容主要讲解“VB.NET实现图像操作的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“VB.NET实现图像操作的方法”吧!
成都创新互联公司服务项目包括崇礼网站建设、崇礼网站制作、崇礼网页制作以及崇礼网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,崇礼网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到崇礼省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
VB.NET最为一款功能强大的.NET编程语言,其实用价值在开发领域是公认的。我们在这里将会为大家介绍一下有关VB.NET图像操作的相关实现技巧,从另一角度去慢慢体会其功能应用的简便及强大性。
慢速,这是以像素点操作为代表:
Public Function fan_slow(ByVal
inputImage As Image) As ImageDim pic As Bitmap =
New Bitmap(inputImage)Dim i As Integer, j As Integer
Dim R As Integer, G As
Integer, B As IntegerDim Width As Integer,
Height As IntegerWidth = Pic.Width :
Height = Pic.HeightDim 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实现图像操作的方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
网页标题:VB.NET实现图像操作的方法
文章起源:http://ybzwz.com/article/pcdpej.html