Solved

Convert C function to VB for me

Posted on 2004-08-28
17
733 Views
Last Modified: 2012-06-21
I need this function to convert a graphic to a 1bpp.  Can anyone rewite this one function to VB.  I've got a little, but I'm a little lost as well.

protected unsafe  void SetIndexedPixel(int  x,int y,BitmapData bmd, bool pixel)
     {
       byte* p=(byte*)bmd.Scan0.ToPointer();
       int  index=y*bmd.Stride+(x>>3);
       byte mask=(byte)(0x80>>(x&0x7));
       if(pixel)
         p[index]|=mask;
       else
         p[index]&=(byte)(mask^0xff);
     }

My VB version wouldstart out something like:

Sub setIndexedPixel(ByVal x as Integer, ByVal y as Integer, ByRef bmd as BitmapData, ByVal pixel As Boolean)
   Dim p As IntPtr=bmd.Scan0

...and there you go - wow I suck.  Need soon.  Any takers?

Original code found at http://www.bobpowell.net/onebit.htm.
0
Comment
Question by:carlajasminelewis
  • 8
  • 4
  • 3
  • +2
17 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 11925625
This code cannot be written in VB .NET. VB doesn't support unsafe programming. byte* p in C# code is not the same thing as IntPtr - this is unsafe pointer to memory region used by C# code. IntPtr is 32-bits pointer used for interoperability. Interoperability features are available both in VB and C#, while unsafe programming is available only in C#.
Compile this function to C# Dll and call it from VB .NET application.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11926191
also there are no shift operations in vb.net which would make this code kind of fun :) I guess you could use mult/divs by power of 2  ...

I agree with alex though, compile it in C# and add the reference to your vb app

0
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11928868
Use BitArray and BitConverter classes for bitwise operations.
Use Marshal.Copy Method (IntPtr, Byte[], Int32, Int32) for copying from IntPtr to Byte()
Also take a look at ms-help://MS.MSDNQTR.2004JUL.1033/vbls7/html/vblrfVBSpec11_11.htm
Visual Basic Language Specification   - 11.18 Shift Operators
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11931507
Alex or Greggory:
Would one of you care to compile the function as a DLL that I can call from a VB.Net program?
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11931519
Gregory:  there are shift operators in VB
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931601
sure email me ... this name at experts-exchange ill email it back.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931670
but ill need a dll with the things it references to build it
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11931786
Really, it is only this one function.  You see, I'm trying to save a 24bpp image as a 1bpp image.  I have thermal printer that will only display 1bpp images.  It's a long story.  Someone forwarded me this link: http://www.bobpowell.net/onebit.htm .  In two functions, this guy does exactly what I need, except his is in C#.  The other function is easily migratable.

I belive the only reference it would need would be: "using System.Drawing;"

It might be worth it to take a look at the code in the link above.  It is a very simple program.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11932228
ah ok

drop me an email ill send it back.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11932261
its built and awaiting you.

I used the namespace carlajasminelewis class is class1 unless you want something else.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11952127
This shift operators << and >> are only in VB.NET 2003.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11952137
Hopefully, we will hear what the solution was, right?

Bob
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11952270
maybe ... I emailed it ..
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11959217
I realized I could do that myself as well.  Thanks for all your help.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11960576
this should at the least go to Bob as well.

If you like I would be happy to reopen the question.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11962418
No, it should go to everyone that reads this question.  Emails are against E-E guidelines.

Bob
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11963597
the email was just the code here compiled not further info, you are the one who actually answerred the question therefor you should get the points as stated above.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question