473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

//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_fillcolorcol or);
m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolo r),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,strid e)),startcolor) &&
(!(PixelsChecked[i,y+1])))
LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
ptr+=1;
}

}
Jul 26 '06 #1
4 2150
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" <ab*@123.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
//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_fillcolorcol or);
m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolo r),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,strid e)),startcolor) &&
(!(PixelsChecked[i,y+1])))
LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
ptr+=1;
}

}


Jul 26 '06 #2
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" <dr*******************@hotmail.comschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
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" <ab*@123.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>//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_fillcolorco lor);
m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcol or),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,stri de)),startcolor) &&
(!(PixelsChecked[i,y+1])))
LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
ptr+=1;
}

}



Jul 26 '06 #3
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_fillcolorcol or);
m_fillcolor=BGRA(GetB(m_fillcolor),GetG(m_fillcolo r),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,strid e)),startcolor) &&
(!(PixelsChecked[i,y+1])))
LinearFloodFill4(scan0, i,y+1,bmpsize,stride,startcolor);
ptr+=1;
}

}
Jul 26 '06 #4

"Cor Ligthert [MVP]" <no************@planet.nlдÈëÏûÏ¢
news:O%****************@TK2MSFTNGP04.phx.gbl...
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
Jul 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
3
by: Motion Musso aka: Sathia | last post by:
function up(bpid) { image = document.getElementById('up_'+bpid); image.setAttribute('SRC','/templates/images/yespulse.gif') up_down('/up_down.php?bpid=' + bpid + '&action=up'); ...
5
by: Zeke Zinzul | last post by:
Hi, I am writing a simple graphics program to generate a Yin Yang logo. I need to implement a flood fill routine and am trying to write a stack class in C#. My main program is in VB. My...
10
by: Thirsty Traveler | last post by:
All of the XmlReader examples assume that the XmlDocument is being read from a file. However, I would like to use it with an XmlDocument passed as a method parameter, but am not sure how. Any...
4
by: eking | last post by:
//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...
1
Curtis Rutland
by: Curtis Rutland | last post by:
How To Use A Database In Your Program Part II This article is intended to extend Frinny’s excellent article: How to Use a Database in Your Program. Frinny’s article defines the basic concepts...
1
by: Alexio | last post by:
I have two similar function that need to be combined. Individually they both work however combined one over rides the other. The code is attached below and an attachment is included for data...
15
by: learner247 | last post by:
Hi, I am learning csharp and have a question: My question is about handling a receiving socket. I use the backgroundworker class for multithreading. In the doWork event there is a while...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.