שתף באמצעות


Setting the colour of pixels in a picture box

Question

Tuesday, July 21, 2009 9:36 AM

I have been reading up on ways to write pixels, and the ways make sense, but i can't seem to do it, i would like to write just a singl pixel at cartesian coordinate (0,0) Do i need to declare the pixel first and then rite it? Or is there an equivilent function to Pset?
Below is the vb6 way of doing it. do i need to declare Pset and then use an instance of Pset to do the same thing in .net?

frmXML.picXML.Pset(0, 0)

If you're not living on the edge, you're taking up too much room

All replies (9)

Tuesday, July 21, 2009 3:59 PM ✅Answered

Miller,

All you have to do is create a graphics object using the PictureBox:

            Dim g As Graphics = PictureBox1.CreateGraphics
            g.DrawEllipse(Pens.Black, 0, 0, 200, 100)

No need to write an all new function. However, I don't know if this is exatly what your looking for. I would investigate the method suggested by Waleed.Programming is easy, understanding how is not.


Tuesday, July 21, 2009 9:49 AM

I have read up and i need to use DrawElipse, what function is drawelipse a member off? i have so far system.windows.forms.picturebox.(tried to put draw elipse here) error came up drawelipse is not a member of system.windows.forms.pictureboxIf you're not living on the edge, you're taking up too much room


Tuesday, July 21, 2009 9:52 AM

Is tehre anyway of doing this without creating a whole new sub that looks like this?

Public Sub DrawEllipseInt(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create location and size of ellipse.
    Dim x As Integer = 0
    Dim y As Integer = 0
    Dim width As Integer = 200
    Dim height As Integer = 100

    ' Draw ellipse to screen.
    e.Graphics.DrawEllipse(blackPen, x, y, width, height)
End Sub

If you're not living on the edge, you're taking up too much room


Tuesday, July 21, 2009 9:55 AM

Hello Miller,

Do you want to change the color of a pixel?
Do you use VB.NET or VB6?Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology


Tuesday, July 21, 2009 9:59 AM

*Using VB.Net*

I would like to create a pixel and then from the one pixel make a table.

If you're not living on the edge, you're taking up too much room


Tuesday, July 21, 2009 10:17 AM | 1 vote

Why don't you access pixels using GetPixel and SetPixel Methods?

this is an example to do so,

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class GetPixelSetPixel
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

public class Form1
  Inherits System.Windows.Forms.Form

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

        Dim curBitmap As New Bitmap("Mailer.jpg")

        g.DrawImage(curBitmap, 0, 0, curBitmap.Width, curBitmap.Height)

        Dim startTime As DateTime = DateTime.Now

        Dim i As Integer
        For i = 0 To 300
            Dim j As Integer
            For j = 0 To 200
                Dim curColor As Color = curBitmap.GetPixel(i, j)
                Dim ret As Integer = CInt((CInt(curColor.R) + CInt(curColor.G) + CInt(curColor.B)) / 3)
                curBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret))
            Next j
        Next i

        g.DrawImage(curBitmap, 0, 0, curBitmap.Width, curBitmap.Height)
        g.Dispose()
  End Sub

  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

  End Sub

End Class

If you lack performance, you may use Lockbits and unlock bits methods which locks memory access to picture till you finish your processing and then call unlockbits method.

You may check this example:
http://www.vb-helper.com/howto_net_lockbits_image_class.html


Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology


Tuesday, July 21, 2009 1:45 PM

Do i need to replace the word PSet with all this code, or do i write the code in another .vb file and call to it in place of the word PSet?

If you're not living on the edge, you're taking up too much room


Tuesday, July 21, 2009 2:24 PM

No Miller, this was just an example to show hoe you can iterate through all pixels in an image.

If you want to set a group of pixels color you can use these few lines of code:

 'Bitmap Object to store your picturebox image
        Dim bmp As New Bitmap(1, 1)

        'Store the current image in a the bitmap object (assuming you have a picturebox on your form named picturebox1)
        bmp = PictureBox1.Image

        'Change portion of image colour (70 X 100 pixels just as a demo)
        For i = 0 To 70
            For j = 0 To 100

                'change Alpha, Red, Green and blue values
                bmp.SetPixel(i, j, Color.FromArgb(100, 100, 100, 100))

            Next

        Next

        'Display Changed Image back to picturebox control
        PictureBox1.Image = bmp

Regards
Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology


Wednesday, July 22, 2009 11:24 AM

PEng1,

or anyone else, the dimensions 200, 100 on the end of the draweclipse function they are pixel dimensions, can that be set to 1, 1 so that the eclipse is only 1 square pixel? Or will this sting me later when i am told there is an error related to the size of the eclipse?

If you're not living on the edge, you're taking up too much room