迅速实现VB.NET图像操作方法简介

开发 后端
VB.NET图像操作可以分为慢速及快速等方法来进行实现。实际的操作方法应当按照我们特有的需求进行正确的选择。下面就一一分析各种方法的应用。

VB.NET最为一款功能强大的.NET编程语言,其实用价值在开发领域是公认的。我们在这里将会为大家介绍一下有关VB.NET图像操作的相关实现技巧,从另一角度去慢慢体会其功能应用的简便及强大性。 #t#

慢速,这是以像素点操作为代表:

  1. Public Function fan_slow(ByVal 
    inputImage As Image) As Image   
  2. Dim pic As Bitmap = 
    New Bitmap(inputImage)   
  3. Dim i As Integer, j As Integer   
  4. Dim R As Integer, G As 
    Integer, B As Integer   
  5. Dim Width As Integer, 
    Height As Integer   
  6. Width = Pic.Width : 
    Height = Pic.Height   
  7. Dim myColor As Color   
  8. For i = 0 To Height - 1   
  9. For j = 0 To Width - 1   
  10. R = 255-pic.GetPixel(i, j).R   
  11. G = 255-pic.GetPixel(i, j).G   
  12. B = 255-pic.GetPixel(i, j).B   
  13. myColor = Color.FromArgb(R, G, B)   
  14. pic.SetPixel(i, j, myColor)   
  15. Next   
  16. Next   
  17. Return pic   
  18. End Function  

快速,以内存指针操作为代表,这是VB.NET图像操作中最快的方法

Public Function fan_fast(ByVal 
inputImage As Image) As Image   
Dim R As Byte, G As Byte, B As 
Byte, Col As Byte   
Dim Width As Integer, Height 
As Integer   
Dim Pic As Bitmap = New 
Bitmap(inputImage)   
Width = Pic.Width : 
Height = Pic.Height    Dim 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 * Height    Dim rgbValues(bytes - 1) As Byte    Marshal.Copy(ptr, rgbValues, 0, bytes)
 '将内存块复制到数组,这是该方法的关键   
For k As Integer = 0 To 
rgbValues.Length - 4 Step 4   
B = 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;    }    }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

如果要改造成vb.net,就是这样,VB.NET图像操作的速度大约比数组加指针慢2-3倍

Public Function fan_fast2(ByVal 
inputImage As Image) As Image   
Dim R As Byte, G As Byte, 
B As Byte, Col As Byte   
Dim Width As Integer, 
Height As Integer   
Dim Pic As Bitmap = 
New Bitmap(inputImage)    Width = Pic.Width : Height = 
Pic.Height    Dim 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 * 4   
For 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  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

VB.NET图像操作的相关操作方法就为大家介绍到这里

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-07 11:07:20

VB.NET读取INI

2010-01-07 10:28:04

VB.NET实现接口

2010-01-11 15:43:06

VB.NET类属性

2010-01-11 14:28:14

VB.NET操作Exc

2010-01-12 10:19:02

VB.NET操作GDI

2010-01-21 15:56:31

VB.NET文本框

2010-01-07 10:46:27

VB.NET Sock

2009-10-30 15:37:23

VB.NET Sub创

2010-01-07 15:37:35

VB.NET ForNext循环

2009-10-30 16:40:04

VB.NET Inte

2009-10-29 13:38:05

VB.NET Shar

2010-01-15 17:51:51

VB.NET创建临时文

2009-10-28 17:44:31

VB.NET语言

2009-10-14 13:21:46

VB.NET Acco

2010-01-11 15:54:48

VB.NET操作缩放图

2010-01-15 16:29:47

VB.NET对象存储

2010-01-18 10:26:19

VB.NET中心旋转图

2010-01-08 18:10:44

VB.NET实现任务栏

2010-01-08 18:26:34

VB.NET网页控件事

2009-10-16 11:38:47

VB.NET使用Ali
点赞
收藏

51CTO技术栈公众号