Results 1 to 20 of 20

Thread: Help converting C# to VB [Resolved]

  1. #1

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Help converting C# to VB [Resolved]

    Hi.

    I'm trying to port some image processing code I found on Code Project.

    The code is C#, but I'm trying to port it to VB and there's a line I can figure out.
    I tried the converter on Developerfusion but that couldn't figure it out either, so I'm hoping some of you brainiacs here can crack it

    VB Code:
    1. BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    2. System.IntPtr Scan0 = bmData.Scan0;
    3.  
    4. 'This is the line that bugs me.
    5. byte * p = (byte *)(void *)Scan0

    I believe it creates an array of bytes starting from the memoryhandle that Scan0 points to?!?

    Perhaps I need some marshal, but wouldn't know where to begin.
    Last edited by pax; Jul 19th, 2004 at 06:01 AM.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Could you post the link ? It seems your problem with unsafe code . I know you pax , you always post hard to answer questions !

  3. #3

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    lol
    And I know I can always count on you
    And you're right his code does start with "unsafe"

    Here's a link : http://www.codeproject.com/cs/media/...cfilters11.asp

    The first place he uses it is in his Invert function. I know how to do that with a matrix which is much easier and faster, but he has some other very nice filters which I don't think can be made by matrices.

    At the moment I have solved it by using GetPixel and SetPixel instead of locking it, but the performance sucks.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    The problem is you can't work with pointers in VB.NET , so it's not possible . If your workaround is really working then why not to optimize it(if it's slow) , and use it .

    Or

    Compile that class in C# and use it in your VB.NET proj without converting it .

  5. #5

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    "Not possible..." Not the words I wanted to hear

    Well, the SetPixel and GetPixel works, it's just too slow. And I don't see a way to optimize it. There seems to be a performance gain when using the lockbits and then change the values in memory before releasing them back to the image.

    I just wonder why the LockBits are even available in VB if there's no way to manipulate it.

    I really don't want to just compile and reference it, because I only began re-writing it as a learning experience. (Maybe I could/should start a new learning experience, learning C#)

    I'll probably put it in the codebank if I ever get it finished. With full credits to the original author, of course.

    Thanks for your help. Appreciate it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    "Not possible..." Not the words I wanted to hear

    Well, the SetPixel and GetPixel works, it's just too slow. And I don't see a way to optimize it. There seems to be a performance gain when using the lockbits and the changed the values in memory before releasing them back to the image.

    I just wonder why the LockBits are even available in VB if there's no way to manipulate it.

    I really don't want to just compile and reference it, because I only began re-writing it as a learning experience. (Maybe I could/should start a new learning experience, learning C#)

    I'll probably put it in the codebank if I ever get it finished. With full credits to the original author, of course.

    Thanks for your help. Appreciate it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  7. #7

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Why the H**L did my last post get posted twice????
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , this could motivate you to learn C# . Now , see one difference between C# and VB.NET .

    As for the performance issue , multithreading and these technique could work sometimes .

  9. #9

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Multithreading, eh? That might be a good idea.

    But I was thinking something else.

    Remember in the good old days we had Peek and Poke?
    You wouldn't happen to know how to do that in VB?
    Because the Scan0 from BitmapData returns the memoryaddress of the bitmap, so if I can't uses pointers, maybe I can Peek and Poke'em.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  10. #10
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Hmm really, multithreading wouldn't help a bit, multithreading doesnt give speed, just puts things in another thread. The differences would be minimal lol

    There is NO way you can get the same speed as in the c# version as vb.net doesn't have pointers. vb.net versions will always be way slower
    \m/\m/

  11. #11

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by PT Exorcist
    Hmm really, multithreading wouldn't help a bit, multithreading doesnt give speed, just puts things in another thread. The differences would be minimal lol
    True, but the UI would at least be responsive...

    But I believe I could get the same performance if I just knew how to read and write to specific memory addresses like peek and poke.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  12. #12
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    You'll have to use the api's lol
    \m/\m/

  13. #13

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Would you happen to know which ones?
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    When I said multithreading I didn't say this from old experience with this specific issue (GDI) but you could try , it wouldn't hurt , I want to know also how things go with it . You can give that thread highest priority while it's doing that operation . I'd agree with PT Exorcist that using C# code would give you performance gain with pointers .

  15. #15

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    I gave up on optimizing it (for now)
    Threading didn't do anything in this class. The users of the class can multithread if they like to keep the UI responsive, but I don't think it something that needs to be in the class itself.

    But I did read the pixels into an array that matches the structure of the BitmapData object, so I could keep the original formulas intact. This will also make it easier to implement peek and poke when(if) I find a solution to it, although it only makes it even slower at the moment. (it' not that bad actually - a couple of sec's for a 1280x1024 image on my machine)

    Anyways, here's what I got so far if you're interested.
    http://www.vbforums.com/showthread.php?threadid=297648


    Thank's for your help guy's.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Good job , pax . I've not tried it yet though . Why don't you work with Mr.Polite since , as I noticed , you're the only two here interested in GDI stuff ?

    Feels good when you get the job done , doesn't it !

  17. #17

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    FYI: The Peek and Poke is actually much eaiser to implement than I first thought.

    System.Runtime.InteropServices.Marshal.ReadByte
    System.Runtime.InteropServices.Marshal.WriteByte

    I have tried a test where I create the array with these functions instead, first looping and reading, manipulating the array and then looping and writting then releasing the bitmapdata.
    It was roughly 3-4 times faster.

    It would probably be even faster if I just use Read/Write bytes directly in the initial loops, but for clarity I think I will keep the arrays and formulas intact.

    Still, 500-600 mSec's for a 1280x1024 image is ok by me.

    I will put it in the codebank when I have implemented it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  18. #18

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Updated version : http://www.vbforums.com/showthread.p...hreadid=297648

    Functions are up to 10 times faster now.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  19. #19
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by pax
    Updated version : http://www.vbforums.com/showthread.p...hreadid=297648
    Functions are up to 10 times faster now.
    And uses managed .NET code , right ?

  20. #20

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    It uses InteropServices, but I didn't make any API declarations, so I guess it's managed( from a certain point of view)

    It uses the Marshal.ReadByte and Marshal.WriteByte.

    I'm not too sure about this, but I guess they are NET wrappers of some Win32 API functions.

    But anyways, I read an article on MSDN about "pure" NET, and they say it's OK (sometimes even better) to use unmanaged code at times.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width