P
Peter Proost
Hi group I already posted this problem yesterday, and I've found a
"solution", but I don't like it, in my opnion there has to be an easier way
to do this, but I can't find, I hope some one can help me with this. Just
copy paste the code in an empty form and press the draw button, then press
the resize button to see what it does, but I just don't like the way I do
it, and I can't seem to find an easier way
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.PictureBox2 = New System.Windows.Forms.PictureBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(34, 42)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(301, 301)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'PictureBox2
'
Me.PictureBox2.Location = New System.Drawing.Point(418, 42)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(140, 140)
Me.PictureBox2.TabIndex = 1
Me.PictureBox2.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(94, 10)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 2
Me.Button1.Text = "Draw"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(198, 10)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 22)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Resize"
Me.Button2.Visible = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(740, 419)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2,
Me.Button1, Me.PictureBox2, Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim g As Graphics
Dim b As New Bitmap(301, 301)
g = Graphics.FromImage(b)
g.Clear(Color.White)
g.DrawLine(New Pen(Color.Black), 0, 0, 300, 0)
g.DrawLine(New Pen(Color.Black), 0, 300, 0, 0)
g.DrawLine(New Pen(Color.Black), 300, 0, 300, 300)
g.DrawLine(New Pen(Color.Black), 300, 300, 0, 300)
PictureBox1.Image = b
g.Dispose()
Button2.Visible = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
Dim g2 As Graphics
Dim scale As Double
scale = 301 / 128
Dim bNew As New Bitmap(CInt(301 / scale) + 1, CInt(301 / scale)
+ 1)
g2 = Graphics.FromImage(bNew)
g2.Clear(Color.White)
g2.PixelOffsetMode =
Drawing.Drawing2D.PixelOffsetMode.HighQuality
g2.DrawImage(PictureBox1.Image, New Rectangle(0, 0, CInt(301 /
scale), CInt(301 / scale)), 0, 0, PictureBox1.Image.Width,
PictureBox1.Image.Height, GraphicsUnit.Pixel)
black(bNew)
PictureBox2.Image = bNew
g2.Dispose()
Catch ex As Exception
ex.ToString()
End Try
End Sub
Private Sub black(ByVal pBitmap As Bitmap)
Dim row, col As Integer
For row = 0 To 128
For col = 0 To 128
If pBitmap.GetPixel(col, row).Name <> "ffffffff" Then
pBitmap.SetPixel(col, row, Color.Black)
End If
Next
Next
End Sub
End Class
"solution", but I don't like it, in my opnion there has to be an easier way
to do this, but I can't find, I hope some one can help me with this. Just
copy paste the code in an empty form and press the draw button, then press
the resize button to see what it does, but I just don't like the way I do
it, and I can't seem to find an easier way
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.PictureBox2 = New System.Windows.Forms.PictureBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(34, 42)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(301, 301)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'PictureBox2
'
Me.PictureBox2.Location = New System.Drawing.Point(418, 42)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(140, 140)
Me.PictureBox2.TabIndex = 1
Me.PictureBox2.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(94, 10)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 2
Me.Button1.Text = "Draw"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(198, 10)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 22)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Resize"
Me.Button2.Visible = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(740, 419)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2,
Me.Button1, Me.PictureBox2, Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim g As Graphics
Dim b As New Bitmap(301, 301)
g = Graphics.FromImage(b)
g.Clear(Color.White)
g.DrawLine(New Pen(Color.Black), 0, 0, 300, 0)
g.DrawLine(New Pen(Color.Black), 0, 300, 0, 0)
g.DrawLine(New Pen(Color.Black), 300, 0, 300, 300)
g.DrawLine(New Pen(Color.Black), 300, 300, 0, 300)
PictureBox1.Image = b
g.Dispose()
Button2.Visible = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
Dim g2 As Graphics
Dim scale As Double
scale = 301 / 128
Dim bNew As New Bitmap(CInt(301 / scale) + 1, CInt(301 / scale)
+ 1)
g2 = Graphics.FromImage(bNew)
g2.Clear(Color.White)
g2.PixelOffsetMode =
Drawing.Drawing2D.PixelOffsetMode.HighQuality
g2.DrawImage(PictureBox1.Image, New Rectangle(0, 0, CInt(301 /
scale), CInt(301 / scale)), 0, 0, PictureBox1.Image.Width,
PictureBox1.Image.Height, GraphicsUnit.Pixel)
black(bNew)
PictureBox2.Image = bNew
g2.Dispose()
Catch ex As Exception
ex.ToString()
End Try
End Sub
Private Sub black(ByVal pBitmap As Bitmap)
Dim row, col As Integer
For row = 0 To 128
For col = 0 To 128
If pBitmap.GetPixel(col, row).Name <> "ffffffff" Then
pBitmap.SetPixel(col, row, Color.Black)
End If
Next
Next
End Sub
End Class