Click here to Skip to main content
15,891,947 members
Home / Discussions / C#
   

C#

 
QuestionConnection between two C# program Pin
Yanshof30-Sep-05 9:13
Yanshof30-Sep-05 9:13 
AnswerRe: Connection between two C# program Pin
DavidNohejl30-Sep-05 9:30
DavidNohejl30-Sep-05 9:30 
QuestionHow to calculate the nearest color? Pin
bouli30-Sep-05 9:07
bouli30-Sep-05 9:07 
AnswerRe: How to calculate the nearest color? Pin
DavidNohejl30-Sep-05 9:27
DavidNohejl30-Sep-05 9:27 
QuestionCSharpCodeProvider generated EXE embedded with ResourceWriter generated .resources fails... Pin
Jon Rista30-Sep-05 6:43
Jon Rista30-Sep-05 6:43 
AnswerRe: CSharpCodeProvider generated EXE embedded with ResourceWriter generated .resources fails... Pin
Jon Rista1-Oct-05 8:11
Jon Rista1-Oct-05 8:11 
QuestionConvert colour image to Black and White Pin
Colin Waite30-Sep-05 5:57
Colin Waite30-Sep-05 5:57 
AnswerRe: Convert colour image to Black and White Pin
Jon Rista30-Sep-05 7:11
Jon Rista30-Sep-05 7:11 
You would have to operate on each pixel, and set the color for each component (except alpha) to the average of R,G,B. You can do this with GetPixel/SetPixel, but its extremely slow. Using pointers in unsafe code is faster by several magnitudes:

// For 32bit image
public unsafe void ReduceToGrayscale(Bitmap bmp)
{
PixelFormat pf = PixelFormat.Format32bppArgb;
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF rf = m_bitmap.GetBounds(ref unit);
Rectangle r = Rectangle.FromLTRB((int)rf.Left, (int)rf.Top, (int)rf.Right, (int)rf.Bottom);
// OR: Rectangle r = Rectangle.Round(rf);
BitmapData bd = bmp.LockBits(r, ImageLockMode.ReadWrite, pf);

byte* pixel = (byte*)bd.Scan0;
byte r, g, b, avg;
for (int y=0; y<bd.width; y++)
="" {
="" for="" (int="" x="0;" x<bd.height;="" x++)
="" b="*(pixel);
" g="*(pixel+1);
" r="*(pixel+2);

" agv="(r+b+g)/3;

" *(pixel)="avg;
" *(pixel+1)="avg;
" *(pixel+2)="avg;

" *(pixel+3)="0xFF;" alpha

="" pixel="" +="4;
" }
="" }

="" bmp.unlockbits(bd);
}


this="" code="" works="" a="" 32bit="" image.="" 16bit="" image,="" you="" will="" have="" to="" take="" into="" account="" the="" stride="" length.="" bitmap="" image="" in="" .net,="" every="" always="" uses="" enough="" memory="" full="" mage,="" even="" if="" its="" just="" 8bit="" indexed.="" so,="" of="" 555,="" each="" is="" only="" going="" be="" half-full.="" also,="" colors="" are="" diced="" up="" from="" 2="" bytes,="" rather="" than="" 4.="" so="" might="" do="" something="" like="" this="" your="" inner="" loop:

short*="" curcolor;
byte="" r,="" g,="" b,="" avg;
="" ...

curcolor="*(pixel);
b" =="" (curcolor="" &="" 0x1f);
g="((curColor">>5) & 0x1F);
r = ((curColor>>10) & 0x1F);
a = ((curColor>>15) & 0x1);

// Expand color components to 8bit, then average here.
// Several ways to do this, more complicated than I
// can write in a couple minutes.

r = g = b = avg;
a = 1;

// Reduce color components to 5bit...
curColor = 0;
curColor = (short)((a<<15) & (r<<10) & (g<<5) & (b));
*(pixel) = curColor;

pixel++;

Using pointers like this is significantly faster than using GetPixel/SetPixel, and its pretty strait forward code (You'll need to look up the reduction/expansion code for converting 16bit 555 color to 32bit color. There are plenty of resources on the internet with the info. I'd write it myself, but I have limited time.) Hope this helps, and I hope it was Grayscale you were after. Reduction to true B/W (two color, not 256 shades of gray) is a bit more complicated.
QuestionConvert a video from 32bit -&gt;16bit in DirectShow -- possible? Pin
Nyst30-Sep-05 5:12
Nyst30-Sep-05 5:12 
QuestionRetrieve resource from resources.resx? Pin
Carl Mercier30-Sep-05 4:04
Carl Mercier30-Sep-05 4:04 
AnswerRe: Retrieve resource from resources.resx? Pin
Jon Rista30-Sep-05 7:23
Jon Rista30-Sep-05 7:23 
AnswerRe: Retrieve resource from resources.resx? Pin
Heinz_30-Sep-05 8:33
Heinz_30-Sep-05 8:33 
GeneralRe: Retrieve resource from resources.resx? Pin
Carl Mercier30-Sep-05 10:56
Carl Mercier30-Sep-05 10:56 
GeneralRe: Retrieve resource from resources.resx? Pin
Heinz_30-Sep-05 17:35
Heinz_30-Sep-05 17:35 
QuestionPage_Unload question Pin
Sled Dog30-Sep-05 3:50
Sled Dog30-Sep-05 3:50 
AnswerRe: Page_Unload question Pin
Tom Larsen30-Sep-05 4:35
Tom Larsen30-Sep-05 4:35 
GeneralRe: Page_Unload question Pin
Sled Dog30-Sep-05 4:56
Sled Dog30-Sep-05 4:56 
QuestionCrystalReport RAS Request Pin
User 209307330-Sep-05 2:26
User 209307330-Sep-05 2:26 
Questionfetching data from dataset using sql query Pin
arusmemon30-Sep-05 1:40
arusmemon30-Sep-05 1:40 
AnswerRe: fetching data from dataset using sql query Pin
Gavin Jeffrey30-Sep-05 2:07
Gavin Jeffrey30-Sep-05 2:07 
AnswerRe: fetching data from dataset using sql query Pin
Jon Rista30-Sep-05 7:36
Jon Rista30-Sep-05 7:36 
QuestionRotating images Pin
PHDENG8130-Sep-05 1:25
PHDENG8130-Sep-05 1:25 
AnswerRe: Rotating images Pin
Robert Rohde30-Sep-05 9:55
Robert Rohde30-Sep-05 9:55 
Questionhow to pass messages between java and .net without using sockets Pin
hasansheik30-Sep-05 1:03
hasansheik30-Sep-05 1:03 
AnswerRe: how to pass messages between java and .net without using sockets Pin
mohamad rafi30-Sep-05 3:01
sussmohamad rafi30-Sep-05 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.