943,929 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2549
  • VB.NET RSS
May 5th, 2008
0

Converting from C# .NET 1.0 to VB.NET 2.0

Expand Post »
Hi!

It's been a while since i last posted, and now I'm in need of some expert assistance.

I've found this Control written in C# with .NET 1.0 on vbAccelerator and wanted to use it with my current project. I decided to convert the entire code to VB.NET but one of the subs contains "pointer code".

I know you can use combinations of IntPtr, Marshal and GCHandle to emulate(?) pointer handling, but I don't know how.
I would very much appreciate some help with converting this portion of the code.
More specifically those lines that contain "pointer code" (marked with red).

BitmapData bmData = glyph.LockBits(
            new Rectangle(0, 0, glyph.Width, glyph.Height),
            ImageLockMode.ReadWrite,
            PixelFormat.Format24bppRgb);
         IntPtr scan0 = bmData.Scan0;
         int nOffset = bmData.Stride - glyph.Width * 3;

         unsafe
         {
            byte * pDest = (byte *)(void *)scan0;

            for (int y = 0; y < glyph.Height; ++y)
            {
               for (int x = 0; x < glyph.Width; ++x)
               {
                  // Check whether transparent:
                  if (transColor.R == pDest[2] && 
                     transColor.G == pDest[1] &&
                     transColor.B == pDest[0])
                  {
                     // set to background colour
                     pDest[2] = backColor.R;
                     pDest[1] = backColor.G;
                     pDest[0] = backColor.B;
                  }
                  else
                  {
                     // Get HLS of existing colour:
                     Color pixel = Color.FromArgb(pDest[2], pDest[1], pDest[0]);
                     float lumPixel = pixel.GetBrightness();
                     if (lumPixel <= 0.9F)
                     {
                        float lumOffset = lumPixel / transLuminance;
                        lumPixel = backLuminance * lumOffset;
                        if (lumPixel > 1.0F)
                        {
                           lumPixel = 1.0F;
                        }
                     
                     }
                     // Calculate the new colour
                     HLSRGB newPixel = new HLSRGB(backHue, lumPixel,
                      backSaturation);

                     // set the values:
                     pDest[0] = newPixel.Blue;
                     pDest[1] = newPixel.Green;
                     pDest[2] = newPixel.Red;
                  }
                  // Move to next BGRA
                  pDest += 3;
               }
               pDest += nOffset;
            }
         }
         glyph.UnlockBits(bmData);
Any help or suggestions would be helpful.

Thanks!
Similar Threads
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
May 5th, 2008
2

Re: Converting from C# .NET 1.0 to VB.NET 2.0

vb.net Syntax (Toggle Plain Text)
  1. Dim bmData As BitmapData = glyph.LockBits(New Rectangle(0, 0, glyph.Width, glyph.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  2. Dim scan0 As IntPtr = bmData.Scan0
  3. Dim nOffset As Integer = bmData.Stride - glyph.Width * 3
  4.  
  5. Dim pDest As Byte* = CByte(CType(scan0, void*))
  6. For y As Integer = 0 To glyph.Height - 1
  7.  
  8. For x As Integer = 0 To glyph.Width - 1
  9. ' Check whether transparent:
  10. If transColor.R = pDest(2) AndAlso transColor.G = pDest(1) AndAlso transColor.B = pDest(0) Then
  11. ' set to background colour
  12. pDest(2) = backColor.R
  13. pDest(1) = backColor.G
  14. pDest(0) = backColor.B
  15. Else
  16. ' Get HLS of existing colour:
  17. Dim pixel As Color = Color.FromArgb(pDest(2), pDest(1), pDest(0))
  18. Dim lumPixel As Single = pixel.GetBrightness()
  19. If lumPixel <= 0.9F Then
  20. Dim lumOffset As Single = lumPixel / transLuminance
  21. lumPixel = backLuminance * lumOffset
  22. If lumPixel > 1F Then
  23. lumPixel = 1F
  24.  
  25. End If
  26. End If
  27. ' Calculate the new colour
  28. Dim newPixel As New HLSRGB(backHue, lumPixel, backSaturation)
  29.  
  30. ' set the values:
  31. pDest(0) = newPixel.Blue
  32. pDest(1) = newPixel.Green
  33. pDest(2) = newPixel.Red
  34. End If
  35. ' Move to next BGRA
  36. pDest += 3
  37. Next
  38. pDest += nOffset
  39. Next
  40.  
  41. glyph.UnlockBits(bmData)
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,140 posts
since Nov 2007
May 5th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

I'm wondering if you have tried this yourself? And it works?
For me, it doesn't work in VB = I've tried before posting.
This gives the error: End of statement expected.
Dim pDest As Byte* = CByte(CType(scan0, void*))
The * indicates that this is a pointer and only works in C++/C#.
These types of declarations are not possible In VB.

I actually put the entire sub through an online conversiontool and that resultet in the very same code you posted.

Currently I'm working with Marshal and a byte array to see if I can get it to work.
Dim pDest As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(scan0))
 pDest = CByte(CType(scan0, Byte))

Suggestions and ideas are welcome.

Thanks!
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
May 5th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

yeah, i convert it in that tools...
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,140 posts
since Nov 2007
May 5th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

Instead of:
Visual Basic Syntax (Toggle Plain Text)
  1. Dim bmData As BitmapData = glyph.LockBits(New Rectangle(0, 0, glyph.Width, glyph.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  2. Dim scan0 As IntPtr = bmData.Scan0
  3. Dim nOffset As Integer = bmData.Stride - glyph.Width * 3
  4. Dim pDest As Byte* = CByte(CType(scan0, void*))
I create a function for setting up my bitmaps, which works in the graphic editor I am writing just fine:
Visual Basic Syntax (Toggle Plain Text)
  1. Dim bmData As BitmapData
  2. Shared stride As Integer
  3. Shared Scan0 As System.IntPtr
  4. Shared nOffset As Integer
  5. Shared nWidth As Integer
  6. Shared bytes As Integer
  7. Shared p() As Byte
  8. Private Shared Function SetUpBitMap(ByVal b As Bitmap) As Boolean
  9. Try
  10. bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  11. stride = bmData.Stride
  12. Scan0 = bmData.Scan0
  13. nOffset = stride - b.Width * 3
  14. nWidth = b.Width * 3
  15. '
  16. ' Declare an array to hold the bytes of the bitmap.
  17. ' This code is specific to a bitmap with 24 bits per pixels.
  18. '
  19. bytes = b.Width * b.Height * 3
  20. ReDim p(bytes - 1)
  21. '
  22. ' Copy the RGB values into the array.
  23. '
  24. Marshal.Copy(Scan0, p, 0, bytes)
  25. Return True
  26. Catch ex As Exception
  27. MessageBox.Show("Error Fetching Bitmap " & ex.Message)
  28. Return False
  29. End Try
  30. End Function
I know this works because I use it, so give it a try.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
bwkeller is offline Offline
52 posts
since Feb 2008
May 6th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

I tried your solution bwKeller. It works! Thanks a bunch!! :-D

The next part is tricky, because within a nested For loop I need to set 3 colors for each pixel.
VB.NET Syntax (Toggle Plain Text)
  1. For y As Integer = 0 To glyph.Height - 1
  2. For x As Integer = 0 To glyph.Width - 1
  3. If transColor.R = pDest(nPixel + 2) AndAlso transColor.G = pDest(nPixel + 1) AndAlso transColor.B = pDest(nPixel) Then
  4. pDest(nPixel) = backColor.B
  5. pDest(nPixel + 1) = backColor.G
  6. pDest(nPixel + 2) = backColor.R
  7. End If
  8. nPixel += 3
  9. Next
  10. nPixel += nOffset
  11. Next
However, the byte array only becomes 1083 (give or take a few) in size and pDest(nPixel + 1) is larger than that, I get an Index out of bounds error.

I'm guessing the array needs to be increased in size by a factor of nOffset so I'm just gonna test that and see what happens.
The array will probably be larger then needed. Would that screw up the bitmap?
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006
May 6th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

Here is a function I use in conjunction with the previous SetUpBitMap() function to convert a graphic to grayscale. I think it should help you solve your issue:

vb.net Syntax (Toggle Plain Text)
  1. Public Shared Function GrayScale(ByVal b As Bitmap) As Boolean
  2. If SetUpBitMap(b) Then
  3. Dim red As Byte, green As Byte, blue As Byte, Gray As Byte
  4. For counter As Integer = 0 To p.Length - 1 Step 3
  5. blue = p(counter)
  6. green = p(counter + 1)
  7. red = p(counter + 2)
  8. Gray = CByte((0.299 * red + 0.587 * green + 0.114 * blue))
  9. p(counter) = Gray
  10. p(counter + 1) = Gray
  11. p(counter + 2) = Gray
  12. Next
  13. '
  14. ' Copy the RGB values back to the bitmap
  15. '
  16. System.Runtime.InteropServices.Marshal.Copy(p, 0, Scan0, bytes)
  17. '
  18. ' Unlock the bitmap
  19. '
  20. b.UnlockBits(bmData)
  21. Return True
  22. End If
  23. End Function
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
bwkeller is offline Offline
52 posts
since Feb 2008
May 6th, 2008
0

Re: Converting from C# .NET 1.0 to VB.NET 2.0

Thanks!
It works perfectly!!

This problem has now been solved...
Reputation Points: 87
Solved Threads: 128
Practically a Master Poster
Oxiegen is offline Offline
652 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Using Dataset in OledbConnection
Next Thread in VB.NET Forum Timeline: Program loses response during large DB queries VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC