Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert C function to VB for me

Posted on 2004-08-28
17
734 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
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.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

766 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