PC Review


Reply
Thread Tools Rate Thread

Anyone can help me?£¬How to Convert C# to VB.NET, about Flood Fill

 
 
eking
Guest
Posts: n/a
 
      26th Jul 2006
//Thanks for any help,thank you!лл¡£

public override void FloodFill(Bitmap bmp, Point pt)
{
int ctr=timeGetTime();

//Debug.WriteLine("*******Flood Fill******");

//get the color's int value, and convert it from RGBA to BGRA format (as
GDI+ uses BGRA)
m_fillcolor=ColorTranslator.ToWin32(m_fillcolorcolor);
m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolor),GetR(m_fillcolor),GetA(m_fillcolor));

//get the bits
BitmapData bmpData=bmp.LockBits(new
Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb);
System.IntPtr Scan0 = bmpData.Scan0;

unsafe
{
//resolve pointer
byte * scan0=(byte *)(void *)Scan0;
//get the starting color
//[loc += Y offset + X offset]
int
loc=CoordsToIndex(pt.X,pt.Y,bmpData.Stride);//((bmpData.Stride*(pt.Y-1))+(pt.X*4));
int color= *((int*)(scan0+loc));

//create the array of bools that indicates whether each pixel
//has been checked. (Should be bitfield, but C#
doesn't support bitfields.)
PixelsChecked=new
bool[bmpData.Width+1,bmpData.Height+1];

//do the first call to the loop

LinearFloodFill4(scan0,pt.X,pt.Y,new
Size(bmpData.Width,bmpData.Height),bmpData.Stride,(byte*)&color);

}

bmp.UnlockBits(bmpData);

m_TimeBenchmark=timeGetTime()-ctr;

}

//***********
//LINEAR ALGORITHM
//***********

unsafe void LinearFloodFill4( byte* scan0, int x, int y,Size bmpsize, int
stride, byte* startcolor)
{

//offset the pointer to the point passed in
int* p=(int*) (scan0+(CoordsToIndex(x,y, stride)));


//FIND LEFT EDGE OF COLOR AREA
int LFillLoc=x; //the location to check/fill on the left
int* ptr=p; //the pointer to the current location
while(true)
{
ptr[0]=m_fillcolor; //fill with the color
PixelsChecked[LFillLoc,y]=true;
LFillLoc--; //de-increment counter
ptr-=1; //de-increment pointer
if(LFillLoc<=0 || !CheckPixel((byte*)ptr,startcolor) ||
(PixelsChecked[LFillLoc,y]))
break; //exit loop if we're at edge of bitmap or color area

}
LFillLoc++;

//FIND RIGHT EDGE OF COLOR AREA
int RFillLoc=x; //the location to check/fill on the left
ptr=p;
while(true)
{
ptr[0]=m_fillcolor; //fill with the color
PixelsChecked[RFillLoc,y]=true;
RFillLoc++; //increment counter
ptr+=1; //increment pointer
if(RFillLoc>=bmpsize.Width || !CheckPixel((byte*)ptr,startcolor) ||
(PixelsChecked[RFillLoc,y]))
break; //exit loop if we're at edge of bitmap or color area

}
RFillLoc--;


//START THE LOOP UPWARDS AND DOWNWARDS
ptr=(int*)(scan0+CoordsToIndex(LFillLoc,y,stride));
for(int i=LFillLoc;i<=RFillLoc;i++)
{
//START LOOP UPWARDS
//if we're not above the top of the bitmap and the pixel above this one is
within the color tolerance
if(y>0 && CheckPixel((byte*)(scan0+CoordsToIndex(i,y-1,stride)),startcolor)
&& (!(PixelsChecked[i,y-1])))
LinearFloodFill4(scan0, i,y-1,bmpsize,stride,startcolor);
//START LOOP DOWNWARDS
if(y<(bmpsize.Height-1) &&
CheckPixel((byte*)(scan0+CoordsToIndex(i,y+1,stride)),startcolor) &&
(!(PixelsChecked[i,y+1])))
LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
ptr+=1;
}

}


 
Reply With Quote
 
 
 
 
Greg Young
Guest
Posts: n/a
 
      26th Jul 2006
This code is not a straight forward translation as the C# version is using
unsafe code.

VB>NET does not support unsafe code .. to remove the unsafe code would
change drastically the performance of the algorithm. I would probably leave
it in C# if that is at all an option.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"eking" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> //Thanks for any help,thank you!лл¡£
>
> public override void FloodFill(Bitmap bmp, Point pt)
> {
> int ctr=timeGetTime();
>
> //Debug.WriteLine("*******Flood Fill******");
>
> //get the color's int value, and convert it from RGBA to BGRA format (as
> GDI+ uses BGRA)
>
> m_fillcolor=ColorTranslator.ToWin32(m_fillcolorcolor);
> m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolor),GetR(m_fillcolor),GetA(m_fillcolor));
>
> //get the bits
> BitmapData bmpData=bmp.LockBits(new
> Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb);
> System.IntPtr Scan0 = bmpData.Scan0;
>
> unsafe
> {
> //resolve pointer
> byte * scan0=(byte *)(void *)Scan0;
> //get the starting color
> //[loc += Y offset + X offset]
> int
> loc=CoordsToIndex(pt.X,pt.Y,bmpData.Stride);//((bmpData.Stride*(pt.Y-1))+(pt.X*4));
> int color= *((int*)(scan0+loc));
>
> //create the array of bools that indicates whether each pixel
> //has been checked. (Should be bitfield, but C#
> doesn't support bitfields.)
> PixelsChecked=new
> bool[bmpData.Width+1,bmpData.Height+1];
>
> //do the first call to the loop
>
> LinearFloodFill4(scan0,pt.X,pt.Y,new
> Size(bmpData.Width,bmpData.Height),bmpData.Stride,(byte*)&color);
>
> }
>
> bmp.UnlockBits(bmpData);
>
> m_TimeBenchmark=timeGetTime()-ctr;
>
> }
>
> //***********
> //LINEAR ALGORITHM
> //***********
>
> unsafe void LinearFloodFill4( byte* scan0, int x, int y,Size bmpsize, int
> stride, byte* startcolor)
> {
>
> //offset the pointer to the point passed in
> int* p=(int*) (scan0+(CoordsToIndex(x,y, stride)));
>
>
> //FIND LEFT EDGE OF COLOR AREA
> int LFillLoc=x; //the location to check/fill on the left
> int* ptr=p; //the pointer to the current location
> while(true)
> {
> ptr[0]=m_fillcolor; //fill with the color
> PixelsChecked[LFillLoc,y]=true;
> LFillLoc--; //de-increment counter
> ptr-=1; //de-increment pointer
> if(LFillLoc<=0 || !CheckPixel((byte*)ptr,startcolor) ||
> (PixelsChecked[LFillLoc,y]))
> break; //exit loop if we're at edge of bitmap or color area
>
> }
> LFillLoc++;
>
> //FIND RIGHT EDGE OF COLOR AREA
> int RFillLoc=x; //the location to check/fill on the left
> ptr=p;
> while(true)
> {
> ptr[0]=m_fillcolor; //fill with the color
> PixelsChecked[RFillLoc,y]=true;
> RFillLoc++; //increment counter
> ptr+=1; //increment pointer
> if(RFillLoc>=bmpsize.Width || !CheckPixel((byte*)ptr,startcolor) ||
> (PixelsChecked[RFillLoc,y]))
> break; //exit loop if we're at edge of bitmap or color area
>
> }
> RFillLoc--;
>
>
> //START THE LOOP UPWARDS AND DOWNWARDS
> ptr=(int*)(scan0+CoordsToIndex(LFillLoc,y,stride));
> for(int i=LFillLoc;i<=RFillLoc;i++)
> {
> //START LOOP UPWARDS
> //if we're not above the top of the bitmap and the pixel above this one is
> within the color tolerance
> if(y>0 &&
> CheckPixel((byte*)(scan0+CoordsToIndex(i,y-1,stride)),startcolor)
> && (!(PixelsChecked[i,y-1])))
> LinearFloodFill4(scan0, i,y-1,bmpsize,stride,startcolor);
> //START LOOP DOWNWARDS
> if(y<(bmpsize.Height-1) &&
> CheckPixel((byte*)(scan0+CoordsToIndex(i,y+1,stride)),startcolor) &&
> (!(PixelsChecked[i,y+1])))
> LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
> ptr+=1;
> }
>
> }
>
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      26th Jul 2006
As Addition to Greg,

Where an option can be to create a small C# project as DLL library, to Add
that as existing to your project and set a Project Reference using the
Project -> Add References -> Projects in your VBNet project.

Cor


"Greg Young" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
> This code is not a straight forward translation as the C# version is using
> unsafe code.
>
> VB>NET does not support unsafe code .. to remove the unsafe code would
> change drastically the performance of the algorithm. I would probably
> leave it in C# if that is at all an option.
>
> Cheers,
>
> Greg Young
> MVP - C#
> http://codebetter.com/blogs/gregyoung
>
> "eking" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> //Thanks for any help,thank you!лл¡£
>>
>> public override void FloodFill(Bitmap bmp, Point pt)
>> {
>> int ctr=timeGetTime();
>>
>> //Debug.WriteLine("*******Flood Fill******");
>>
>> //get the color's int value, and convert it from RGBA to BGRA format (as
>> GDI+ uses BGRA)
>>
>> m_fillcolor=ColorTranslator.ToWin32(m_fillcolorcolor);
>> m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolor),GetR(m_fillcolor),GetA(m_fillcolor));
>>
>> //get the bits
>> BitmapData bmpData=bmp.LockBits(new
>> Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb);
>> System.IntPtr Scan0 = bmpData.Scan0;
>>
>> unsafe
>> {
>> //resolve pointer
>> byte * scan0=(byte *)(void *)Scan0;
>> //get the starting color
>> //[loc += Y offset + X offset]
>> int
>> loc=CoordsToIndex(pt.X,pt.Y,bmpData.Stride);//((bmpData.Stride*(pt.Y-1))+(pt.X*4));
>> int color= *((int*)(scan0+loc));
>>
>> //create the array of bools that indicates whether each pixel
>> //has been checked. (Should be bitfield, but C#
>> doesn't support bitfields.)
>> PixelsChecked=new
>> bool[bmpData.Width+1,bmpData.Height+1];
>>
>> //do the first call to the loop
>>
>> LinearFloodFill4(scan0,pt.X,pt.Y,new
>> Size(bmpData.Width,bmpData.Height),bmpData.Stride,(byte*)&color);
>>
>> }
>>
>> bmp.UnlockBits(bmpData);
>>
>> m_TimeBenchmark=timeGetTime()-ctr;
>>
>> }
>>
>> //***********
>> //LINEAR ALGORITHM
>> //***********
>>
>> unsafe void LinearFloodFill4( byte* scan0, int x, int y,Size bmpsize, int
>> stride, byte* startcolor)
>> {
>>
>> //offset the pointer to the point passed in
>> int* p=(int*) (scan0+(CoordsToIndex(x,y, stride)));
>>
>>
>> //FIND LEFT EDGE OF COLOR AREA
>> int LFillLoc=x; //the location to check/fill on the left
>> int* ptr=p; //the pointer to the current location
>> while(true)
>> {
>> ptr[0]=m_fillcolor; //fill with the color
>> PixelsChecked[LFillLoc,y]=true;
>> LFillLoc--; //de-increment counter
>> ptr-=1; //de-increment pointer
>> if(LFillLoc<=0 || !CheckPixel((byte*)ptr,startcolor) ||
>> (PixelsChecked[LFillLoc,y]))
>> break; //exit loop if we're at edge of bitmap or color area
>>
>> }
>> LFillLoc++;
>>
>> //FIND RIGHT EDGE OF COLOR AREA
>> int RFillLoc=x; //the location to check/fill on the left
>> ptr=p;
>> while(true)
>> {
>> ptr[0]=m_fillcolor; //fill with the color
>> PixelsChecked[RFillLoc,y]=true;
>> RFillLoc++; //increment counter
>> ptr+=1; //increment pointer
>> if(RFillLoc>=bmpsize.Width || !CheckPixel((byte*)ptr,startcolor) ||
>> (PixelsChecked[RFillLoc,y]))
>> break; //exit loop if we're at edge of bitmap or color area
>>
>> }
>> RFillLoc--;
>>
>>
>> //START THE LOOP UPWARDS AND DOWNWARDS
>> ptr=(int*)(scan0+CoordsToIndex(LFillLoc,y,stride));
>> for(int i=LFillLoc;i<=RFillLoc;i++)
>> {
>> //START LOOP UPWARDS
>> //if we're not above the top of the bitmap and the pixel above this one
>> is
>> within the color tolerance
>> if(y>0 &&
>> CheckPixel((byte*)(scan0+CoordsToIndex(i,y-1,stride)),startcolor)
>> && (!(PixelsChecked[i,y-1])))
>> LinearFloodFill4(scan0, i,y-1,bmpsize,stride,startcolor);
>> //START LOOP DOWNWARDS
>> if(y<(bmpsize.Height-1) &&
>> CheckPixel((byte*)(scan0+CoordsToIndex(i,y+1,stride)),startcolor) &&
>> (!(PixelsChecked[i,y+1])))
>> LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
>> ptr+=1;
>> }
>>
>> }
>>
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      26th Jul 2006
There is no possible automatic translation of 'unsafe' C# code to VB (this is
also one of the reasons you can't automatically translate C++ code to VB).
The code must be redesigned for VB.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#


"eking" wrote:

> //Thanks for any help,thank you!谢谢。
>
> public override void FloodFill(Bitmap bmp, Point pt)
> {
> int ctr=timeGetTime();
>
> //Debug.WriteLine("*******Flood Fill******");
>
> //get the color's int value, and convert it from RGBA to BGRA format (as
> GDI+ uses BGRA)
> m_fillcolor=ColorTranslator.ToWin32(m_fillcolorcolor);
> m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolor),GetR(m_fillcolor),GetA(m_fillcolor));
>
> //get the bits
> BitmapData bmpData=bmp.LockBits(new
> Rectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb);
> System.IntPtr Scan0 = bmpData.Scan0;
>
> unsafe
> {
> //resolve pointer
> byte * scan0=(byte *)(void *)Scan0;
> //get the starting color
> //[loc += Y offset + X offset]
> int
> loc=CoordsToIndex(pt.X,pt.Y,bmpData.Stride);//((bmpData.Stride*(pt.Y-1))+(pt.X*4));
> int color= *((int*)(scan0+loc));
>
> //create the array of bools that indicates whether each pixel
> //has been checked. (Should be bitfield, but C#
> doesn't support bitfields.)
> PixelsChecked=new
> bool[bmpData.Width+1,bmpData.Height+1];
>
> //do the first call to the loop
>
> LinearFloodFill4(scan0,pt.X,pt.Y,new
> Size(bmpData.Width,bmpData.Height),bmpData.Stride,(byte*)&color);
>
> }
>
> bmp.UnlockBits(bmpData);
>
> m_TimeBenchmark=timeGetTime()-ctr;
>
> }
>
> //***********
> //LINEAR ALGORITHM
> //***********
>
> unsafe void LinearFloodFill4( byte* scan0, int x, int y,Size bmpsize, int
> stride, byte* startcolor)
> {
>
> //offset the pointer to the point passed in
> int* p=(int*) (scan0+(CoordsToIndex(x,y, stride)));
>
>
> //FIND LEFT EDGE OF COLOR AREA
> int LFillLoc=x; //the location to check/fill on the left
> int* ptr=p; //the pointer to the current location
> while(true)
> {
> ptr[0]=m_fillcolor; //fill with the color
> PixelsChecked[LFillLoc,y]=true;
> LFillLoc--; //de-increment counter
> ptr-=1; //de-increment pointer
> if(LFillLoc<=0 || !CheckPixel((byte*)ptr,startcolor) ||
> (PixelsChecked[LFillLoc,y]))
> break; //exit loop if we're at edge of bitmap or color area
>
> }
> LFillLoc++;
>
> //FIND RIGHT EDGE OF COLOR AREA
> int RFillLoc=x; //the location to check/fill on the left
> ptr=p;
> while(true)
> {
> ptr[0]=m_fillcolor; //fill with the color
> PixelsChecked[RFillLoc,y]=true;
> RFillLoc++; //increment counter
> ptr+=1; //increment pointer
> if(RFillLoc>=bmpsize.Width || !CheckPixel((byte*)ptr,startcolor) ||
> (PixelsChecked[RFillLoc,y]))
> break; //exit loop if we're at edge of bitmap or color area
>
> }
> RFillLoc--;
>
>
> //START THE LOOP UPWARDS AND DOWNWARDS
> ptr=(int*)(scan0+CoordsToIndex(LFillLoc,y,stride));
> for(int i=LFillLoc;i<=RFillLoc;i++)
> {
> //START LOOP UPWARDS
> //if we're not above the top of the bitmap and the pixel above this one is
> within the color tolerance
> if(y>0 && CheckPixel((byte*)(scan0+CoordsToIndex(i,y-1,stride)),startcolor)
> && (!(PixelsChecked[i,y-1])))
> LinearFloodFill4(scan0, i,y-1,bmpsize,stride,startcolor);
> //START LOOP DOWNWARDS
> if(y<(bmpsize.Height-1) &&
> CheckPixel((byte*)(scan0+CoordsToIndex(i,y+1,stride)),startcolor) &&
> (!(PixelsChecked[i,y+1])))
> LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
> ptr+=1;
> }
>
> }
>
>
>

 
Reply With Quote
 
eking
Guest
Posts: n/a
 
      27th Jul 2006

"Cor Ligthert [MVP]" <(E-Mail Removed)> дÈëÏûÏ¢
news:O%(E-Mail Removed)...
As Addition to Greg,

Where an option can be to create a small C# project as DLL library, to Add
that as existing to your project and set a Project Reference using the


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flood Flood Flood... PotGuy General Discussion 8 20th Jul 2007 11:07 PM
Anyone can help me?£¬How to Convert C# to VB.NET, about Flood Fill eking Microsoft VB .NET 4 27th Jul 2006 02:46 AM
How do I convert a series of underscores to a fill in field? =?Utf-8?B?aGVsbWNq?= Microsoft Word Document Management 1 17th Nov 2005 02:30 PM
How do I convert an excel worksheet to a fill in form? =?Utf-8?B?VGVycmk=?= Microsoft Excel Misc 2 13th Mar 2005 03:26 PM
Flood Fill in VB.NET CF Paul Kay Microsoft Dot NET Compact Framework 4 8th Dec 2003 09:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 PM.