Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
on richTextBox(bachground color black and white forground) i wrote letter y
from the richTextBox i want to get the color of the rectangle size 64X64 pixels.
the problem as you may gussed i could not get a bitmap from the richTextBox
see line A

What I have tried:

C#
Color[,] pixelColor = new Color[100, 100];
//objMyBitMap = new Bitmap(640, //480,System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Bitmap rtb = (Bitmap)richTextBox1.Clone();//<-------  Line A
            for (i = 1; i < 64+1; i++)
            {
                for (j = 1; j < 64+1; j++)
                {
                    //pixelColor[i,j] = objMyBitMap.GetPixel(i, j);
                    pixelColor[i,j] = rtb.GetPixel(i, j);
                }
            }
Posted
Comments
Sergey Alexandrovich Kryukov 6-Feb-16 1:09am    
I wonder why? the idea is correct, except the first line...
Is it System.Windows.Forms.RichTextBox? Always indicate at least one full type name in such questions...
—SA

1 solution

You cannot cast a control into Bitmap. You have to do different thing: copy a part of the screen from the bounding rectangle of the control. Most likely, you are using System.Windows.Forms and System.Drawing. If so, you can use System.Drawing.Graphics.CopyFromScreen:
Graphics.CopyFromScreen Method (Int32, Int32, Int32, Int32, Size) (System.Drawing)[^].

To copy part of the screen into a bitmap, first create a bitmap instance of required size and obtain an instance of System.Drawing.Graphics which can be used to draw in this bitmap: Graphics.FromImage Method (Image) (System.Drawing)[^].

Another problem is: GetPixel is prohibitively slow. Instead, you have to use one of the methods System.Drawing.Bitmap.LockBits:
Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat) (System.Drawing)[^].

Few words on the expected result: it's pretty obvious that RTF document, in a way, does not have its own pixel colors. Its rendering depends on the device, current layout of the bounding rectangle, system settings and other detail. If, say, you use just black text of white background, you will get several colors used for, in particular, anti-aliasing and ClearType effect. It makes me feel puzzled about the purpose of your question.

—SA
 
Share this answer
 
v2
Comments
Engineer khalid 6-Feb-16 4:06am    
Hellow Kryukov
i have looked at your kind solution but now i am at work with more unexpected task. i will try your solution tomorrow and give you the grade of 5
Sergey Alexandrovich Kryukov 6-Feb-16 5:44am    
Sure. And then don't forget to accept the answer formally.
—SA
Engineer khalid 7-Feb-16 0:13am    
Hellow Kryukov
i could copy from the screen but to lock the bits (.LockBits) i must have a bitmap, and no Bitmap could be obtained from the richTextBox (as far as i know)
look at this code
Size s = this.Size;
s.Width=64; s.Height=64;

Bitmap memoryImage = new Bitmap(64,64,myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(1, 1, 1, 1, s);

///
if i could some how obtain the color( any color) from
memoryGraphics
this will be fine with me
otherwise i will print the letter
y (see my question above)
on form1 then use
Bitmap rtb = (Bitmap)form1.Clone();
//
if you have any comment about how to get any colour from
memoryGraphics
please do so , i will return to this site after couple of hours wether you commented or not with a grade of 5
thanks
Sergey Alexandrovich Kryukov 7-Feb-16 1:04am    
In your case, this bitmap is memoryImage. And you should not copy one pixel, you should copy the whole area where your RTF control is.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900