CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #2
    Join Date
    Mar 2010
    Posts
    26

    Post Re: Help reading pixels

    Heres how I do It:
    Code:
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms
    Public Class Form1
        Dim g As System.Drawing.Graphics
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Show()
            Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
            Me.BackgroundImage = System.Drawing.Image.FromFile("C:\Test.bmp")
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim n As Integer, k As Integer
            Dim brush3 As Brush
            Dim mypix2 As Color
            Dim pic As Bitmap ' 
            Dim g As System.Drawing.Graphics
            g = Me.CreateGraphics
            pic = Me.BackgroundImage ' You Can of course say pic=System.Drawing.Image.FromFile("C:\Test.bmp")
            For n = 0 To 400
                For k = 0 To 400
                    mypix2 = pic.GetPixel(n, k) ' The piece of the code relevant to your Needs
                    brush3 = New SolidBrush(Color.FromArgb(mypix2.A, mypix2.R, mypix2.G, mypix2.B))
                    g.FillRectangle(brush3, 0 + n, 400 + k, 1, 1)
                    brush3.Dispose()
                Next k
            Next n
        End Sub
    End Class
    Put this into a project and place a test picture in "C:\Test.bmp"

    Put a Button on the form - the form as big as you can.

    This paints a copy of the image for pixel 0,0 to 400,400
    onto the form 400 pixels down on the y axis.

    This is of course a dumb way to duplicate
    a portion of the bitmap but I like to use little snippets like this to figure out how thing work.

    The two lines of Code you need are to create the bitmap bject and to use it's getpixel method.

    Hope this Helps
    Last edited by Cimperiali; March 8th, 2010 at 03:10 PM. Reason: Added Code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured