Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert C function to VB for me

Posted on 2004-08-28
17
Medium Priority
?
741 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

772 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