|
-
Jul 16th, 2004, 04:36 PM
#1
Thread Starter
Fanatic Member
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:
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
System.IntPtr Scan0 = bmData.Scan0;
'This is the line that bugs me.
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...
-
Jul 16th, 2004, 05:54 PM
#2
Sleep mode
Could you post the link ? It seems your problem with unsafe code . I know you pax , you always post hard to answer questions !
-
Jul 16th, 2004, 06:06 PM
#3
Thread Starter
Fanatic Member
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...
-
Jul 16th, 2004, 06:22 PM
#4
Sleep mode
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 .
-
Jul 16th, 2004, 06:31 PM
#5
Thread Starter
Fanatic Member
"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...
-
Jul 16th, 2004, 06:32 PM
#6
Thread Starter
Fanatic Member
"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...
-
Jul 16th, 2004, 06:33 PM
#7
Thread Starter
Fanatic Member
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...
-
Jul 16th, 2004, 06:39 PM
#8
Sleep mode
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 .
-
Jul 17th, 2004, 08:03 AM
#9
Thread Starter
Fanatic Member
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...
-
Jul 17th, 2004, 09:49 AM
#10
yay gay
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/
-
Jul 17th, 2004, 10:50 AM
#11
Thread Starter
Fanatic Member
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...
-
Jul 17th, 2004, 10:56 AM
#12
yay gay
You'll have to use the api's lol
\m/  \m/
-
Jul 17th, 2004, 12:25 PM
#13
Thread Starter
Fanatic Member
Would you happen to know which ones?
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 17th, 2004, 03:32 PM
#14
Sleep mode
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 .
-
Jul 17th, 2004, 06:21 PM
#15
Thread Starter
Fanatic Member
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...
-
Jul 17th, 2004, 06:31 PM
#16
-
Jul 19th, 2004, 05:37 AM
#17
Thread Starter
Fanatic Member
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...
-
Jul 19th, 2004, 01:56 PM
#18
Thread Starter
Fanatic Member
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...
-
Jul 20th, 2004, 07:53 AM
#19
Sleep mode
And uses managed .NET code , right ?
-
Jul 20th, 2004, 08:39 AM
#20
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|