溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET實現圖像操作的方法

發(fā)布時間:2021-06-17 14:59:35 來源:億速云 閱讀:325 作者:chen 欄目:編程語言

本篇內容主要講解“VB.NET實現圖像操作的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“VB.NET實現圖像操作的方法”吧!

VB.NET最為一款功能強大的.NET編程語言,其實用價值在開發(fā)領域是公認的。我們在這里將會為大家介紹一下有關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實現圖像操作的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI