VB.NET实现图像操作的方法

发布时间:2021-06-17 14:59:35 作者:chen
来源:亿速云 阅读:303

本篇内容主要讲解“VB.NET实现图像操作的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“VB.NET实现图像操作的方法”吧!

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

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

  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图像操作中最快的方法

  1. Public Function fan_fast(ByVal 
    inputImage As Image) As Image   

  2. Dim R As Byte, G As Byte, B As 
    Byte, Col As Byte   

  3. Dim Width As Integer, Height 
    As Integer   

  4. Dim Pic As Bitmap = New 
    Bitmap(inputImage)   

  5. Width = Pic.Width : 
    Height = Pic.Height   

  6. Dim rect As New Rectangle(0, 0, 
    Width, Height)   

  7. Dim bmpData As BitmapData = 
    Pic.LockBits(rect, ImageLockMode.
    ReadWrite, Pic.PixelFormat)   

  8. Dim ptr As IntPtr = bmpData.Scan0
    '得到***个像素的指针   

  9. '数组操作()   

  10. Dim bytes As Integer = 
    bmpData.Stride * Height   

  11. Dim rgbValues(bytes - 1) As Byte   

  12. Marshal.Copy(ptr, rgbValues, 0, bytes)
     '将内存块复制到数组,这是该方法的关键   

  13. For k As Integer = 0 To 
    rgbValues.Length - 4 Step 4   

  14. B = CByte(255 - rgbValues(k))   

  15. G = CByte(255 - rgbValues(k + 1))   

  16. R = CByte(255 - rgbValues(k + 2))   

  17. rgbValues(k) = B   

  18. rgbValues(k + 1) = G   

  19. rgbValues(k + 2) = R   

  20. Next   

  21. Marshal.Copy(rgbValues, 0, ptr, bytes)
    '再将数组复制到内存块   

  22. '数组操作结束   

  23. Pic.UnlockBits(bmpData)   

  24. Return Pic   

  25. End Function   

  26. 还有一种以C#中的非安全代码 指针操作   

  27. public Bitmap fan_fast2(Bitmap b)   

  28. {   

  29. int width = b.Width;   

  30. int height = b.Height;   

  31. BitmapData data = b.LockBits
    (new Rectangle(0, 0, width, height), 
    ImageLockMode.ReadWrite, 
    PixelFormat.Format32bppArgb);   

  32. unsafe   

  33. {   

  34. byte* p = (byte*)data.Scan0;   

  35. int offset = data.Stride - width * 4; 
    for (int y = 0; y < height; y++)   

  36. {   

  37. for (int x = 0; x < width; x++)   

  38. {   

  39. p[2] ^= 0xFF;   

  40. p[1] ^= 0xFF;   

  41. p[0] ^= 0xFF;   

  42. p += 4;   

  43. }   

  44. p += offset;   

  45. }   

  46. b.UnlockBits(data);   

  47. return b;   

  48. }   

  49. }  

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

  1. Public Function fan_fast2(ByVal 
    inputImage As Image) As Image   

  2. Dim R As Byte, G As Byte, 
    B As Byte, Col As Byte   

  3. Dim Width As Integer, 
    Height As Integer   

  4. Dim Pic As Bitmap = 
    New Bitmap(inputImage)   

  5. Width = Pic.Width : Height = 
    Pic.Height   

  6. Dim rect As New Rectangle
    (0, 0, Width, Height)   

  7. Dim bmpData As BitmapData = 
    Pic.LockBits(rect, ImageLockMode.
    ReadWrite, Pic.PixelFormat)   

  8. Dim ptr As IntPtr = bmpData.Scan0
    '得到***个像素的指针   

  9. ''指针操作 在这种模式下,比数组操作要慢2-3倍   

  10. Dim offset As Integer = bmpData.
    Stride - bmpData.Width * 4   

  11. For j As Integer = 0 To Height - 1   

  12. For i As Integer = 0 To Width - 1   

  13. B = CByte(255 - Marshal.ReadByte(ptr))   

  14. G = CByte(255 - Marshal.ReadByte(ptr, 1))   

  15. R = CByte(255 - Marshal.ReadByte(ptr, 2))   

  16. Marshal.WriteByte(ptr, 0, B)   

  17. Marshal.WriteByte(ptr, 1, G)   

  18. Marshal.WriteByte(ptr, 2, R)   

  19. ptr = CType(ptr.ToInt32 + 4, IntPtr)   

  20. Next   

  21. ptr = CType(ptr.ToInt32 + 
    offset, IntPtr)   

  22. Next   

  23. ''指针操作结束   

  24. Pic.UnlockBits(bmpData)   

  25. Return Pic   

  26. End Function  

到此,相信大家对“VB.NET实现图像操作的方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. opencv中图像叠加/图像融合/按位操作的实现案例
  2. VB.NET中心旋转图像的实现方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vb.net

上一篇:Java如何利用文件输入输出流实现文件夹内所有文件拷贝到另一个文件夹

下一篇:vue-router传参的方式有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》