in Education by
I have a 8 bit bitmap color image. when i do a Color pixelcolor = b.GetPixel(j,i); Console.Write(pixelcolor.ToString() + " " ); I get Color [A=255, R=255, G=255, B=255] I need to get only the 8 bit value. not 24 bit seperate values for R,G,B ,A. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There is no way to do this using the Bitmap class directly. However, you can use the LockBits method to access the pixels directly. Using unsafe code: (remember to enable unsafe code in your project first) public static unsafe Byte GetIndexedPixel(Bitmap b, Int32 x, Int32 y) { if (b.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentException("Image is not in 8 bit per pixel indexed format!"); if (x < 0 || x >= b.Width) throw new ArgumentOutOfRangeException("x", string.Format("x should be in 0-{0}", b.Width)); if (y < 0 || y >= b.Height) throw new ArgumentOutOfRangeException("y", string.Format("y should be in 0-{0}", b.Height)); BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat); try { Byte* scan0 = (Byte*)data.Scan0; return scan0[x + y * data.Stride]; } finally { if (data != null) b.UnlockBits(data); } } The safe alternative, using Marshal.Copy: public static Byte GetIndexedPixel(Bitmap b, Int32 x, Int32 y) { if (b.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentException("Image is not in 8 bit per pixel indexed format!"); if (x < 0 || x >= b.Width) throw new ArgumentOutOfRangeException("x", string.Format("x should be in 0-{0}", b.Width)); if (y < 0 || y >= b.Height) throw new ArgumentOutOfRangeException("y", string.Format("y should be in 0-{0}", b.Height)); BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat); try { Byte[] pixel = new Byte[1]; Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + x + y * data.Stride), pixel, 0, 1); return pixel[0]; } finally { if (data != null) b.UnlockBits(data); } }

Related questions

0 votes
    I have a 8 bit bitmap color image. when i do a Color pixelcolor = b.GetPixel(j,i); Console. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I have a 8 bit bitmap color image. when i do a Color pixelcolor = b.GetPixel(j,i); Console. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I have a 8 bit bitmap color image. when i do a Color pixelcolor = b.GetPixel(j,i); Console. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    How to increase the brightness of the pixel? (a) Sound (b) Amount of light (c) Surface (d) Waves Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    Angular js Image upload how to check width and height example my withd=200 and heidht =200 if give ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I've been trying for some time now to get a screen-space pixel (provided by a deferred HLSL ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    name any four tools in Gimp used for changing the pixel arrangement in an image Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    Write a program in python to insert ten friends name in a list, then print 3rd, 5th and 9th value from the list (using data structure) Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    I have an XML document which basically looks like this: SomeSeller 2535 Now I'm trying to parse the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a small calculator that I am creating in C# (Sharp Develop). The user enters two values ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I have a small calculator that I am creating in C# (Sharp Develop). The user enters two values ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am currently using a LINQ query to read an XML file e.g. some_text some_more_text into a list ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    What do you mean by value types and reference types in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) a) [1, 4] b) [1, 3, 4] c) [4, 3] d) [1, 3]...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? print('*', "abcde".center(6), '*', sep='') a) * abcde * b) *abcde * c) * abcde* d) * abcde *...
asked Jan 2, 2023 in Technology by JackTerrance
...