Solved

Convert C function to VB for me

Posted on 2004-08-28
17
732 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now