[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 746
  • Last Modified:

Convert C function to VB for me

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
carlajasminelewis
Asked:
carlajasminelewis
  • 8
  • 4
  • 3
  • +2
1 Solution
 
AlexFMCommented:
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
 
gregoryyoungCommented:
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
 
dimitry_dimitryCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
carlajasminelewisAuthor Commented:
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
 
carlajasminelewisAuthor Commented:
Gregory:  there are shift operators in VB
0
 
gregoryyoungCommented:
sure email me ... this name at experts-exchange ill email it back.
0
 
gregoryyoungCommented:
but ill need a dll with the things it references to build it
0
 
carlajasminelewisAuthor Commented:
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
 
gregoryyoungCommented:
ah ok

drop me an email ill send it back.
0
 
gregoryyoungCommented:
its built and awaiting you.

I used the namespace carlajasminelewis class is class1 unless you want something else.
0
 
Bob LearnedCommented:
This shift operators << and >> are only in VB.NET 2003.

Bob
0
 
Bob LearnedCommented:
Hopefully, we will hear what the solution was, right?

Bob
0
 
gregoryyoungCommented:
maybe ... I emailed it ..
0
 
carlajasminelewisAuthor Commented:
I realized I could do that myself as well.  Thanks for all your help.
0
 
gregoryyoungCommented:
this should at the least go to Bob as well.

If you like I would be happy to reopen the question.
0
 
Bob LearnedCommented:
No, it should go to everyone that reads this question.  Emails are against E-E guidelines.

Bob
0
 
gregoryyoungCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now