How to get pixels from Picturebox

Hi,

I'm developing smart device application by using CF1.0 on VS 2003. I need your help. I've a code for drawing objects (Rect, Circle, String, Image) on PictureBox. what my question is how to copy/get Pixels particular part of area and display its in another PictureBox. Please send your suggestion or links.

Thanx.

M. GANESAN




Answer this question

How to get pixels from Picturebox

  • gabo_uy

    Hi,

    Thanx for your response. But you said only for PictureBox.Image. My requirement is how to get current view (suppose picturebox has drawing objects like rectangle, circle...) from the picturebox. Please send suggestions.

    Thanx.

    M. GANESAN



  • Jandost Khoso

    Hi,

    Yeah i tried your code. it works for drawing images only not for shapes. PictureBox1.Image this is for get/set bitmap image from/to the picturebox. we can't get bitmap and face error if picturebox has no image. Please send your suggestion.

    Thanx.

    M. GANESAN



  • paso

         Sorry, You misunderstood my question. I want to copy particular area of a picture box and display its in another picturebox. OK i'm going to explain briefly.

    I have two picturebox and one button.

             PictureBox1's size is (150,100) and Picturebox2's size is (100,75).

    I have written code for drawing some shapes in the first picturebox at picturebox1_paint() event.

    Private Sub PictureBox1_Paint(........) Handles PictureBox1.Paint

    Dim g As Graphics = e.Graphics

    g.FillEllipse(New SolidBrush(Color.Thistle), 10, 10, 100, 50)

    g.DrawString("Welcome", New Font("arial", 15, FontStyle.Bold), New SolidBrush(Color.Black), 20, 25)

    g.Dispose()

    End Sub

    Now my question is when i clik the command button, half part of picturebox1's Snap has to be displayed in the PictureBox2 (i.e first half ellipse with Welc). Please send your suggestion.

    Note: According to your code. G = Graphics.FromImage(PictureBox1.Image)
    we get an error NullReferenceException if the PictureBox1 has no image but it has some shapes.

    Thanx.

    M. GANESAN

                 



  • blue56789

    Did you try the code , I think it will work even if you draw some shapes on the Picturebox.


  • udayan

    Okay, I want to take a look at the code which you use to draw shapes, because I tested my first code and It worked fine even when I draw some shapes.
    e.g, I used the following method to draw some shapes :
    Dim G As Graphics
    G = Graphics.FromImage(PictureBox1.Image)
    G.FillRectangle(Brushes.Black, 0, 0, 100, 100)


  • Euclidez

    Okay, I understood that
    Take a look at this code, I tested and it worked fine (as you want, EXACTLY)
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    '=====================================================================
    '== To draw an ellipse and string on the first picturebox ==
    '=====================================================================
    PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    'To avoid NullReferenceException if PictureBox1 has no image already
    Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
    g.FillEllipse(New SolidBrush(Color.Thistle), 10, 10, 100, 50)
    g.DrawString("Welcome", New Font("arial", 15, FontStyle.Bold), New SolidBrush(Color.Black), 20, 25)
    g.Dispose()
    '=====================================================================
    '== To copy just a part from the first picturebox to the second one ==
    '=====================================================================
    Dim TmpG As Graphics
    PictureBox2.Image = New Bitmap(PictureBox2.Width, PictureBox2.Height)
    'To avoid NullReferenceException if PictureBox2 has no image already
    TmpG = Graphics.FromImage(PictureBox2.Image)
    TmpG.DrawImage(PictureBox1.Image, New Rectangle(0, 0, 200, 200), New Rectangle(0, 0, 200, 200), GraphicsUnit.Pixel)
    TmpG.Dispose()
    End Sub



  • kastanienreis

    Hi,

    Thank you Moayad Mardini. Yeah your code now it works well. By your code i knew one information what it is how to draw shapes on the picturebox through any event. till now i thought its not possible to draw, and only i draw thru PictureBox1_Paint() event. Again I say thank you.

    Thanx

    M. GANESAN



  • maddman

    If you want to get a pixel from the first picture then set it to the second one :
    Dim SrcBMP As New Bitmap(PictureBox1.Image)
    Dim TrgBMP As New Bitmap(PictureBox2.Image)
    TrgBMP.SetPixel(0, 0, TMPBMP.GetPixel(0, 0)
    Or if you want to get a region from the first picture then set to the second one :
    Dim TmpG As Graphics
    TmpG = Graphics.FromImage(PictureBox2.Image)
    TmpG.DrawImage(PictureBox1.Image, New Rectangle(0, 0, 50, 50), New Rectangle(200, 200, 50, 50), GraphicsUnit.Pixel)


  • How to get pixels from Picturebox