Solved

Convert C function to VB for me

Posted on 2004-08-28
17
738 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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