There has to be an easier way

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
 
C

Cor Ligthert

Peter,

Depends on what you want to do, do you really want to make a smaller picture
or do you want to show the picture in a smaller picturebox.

I use a kind of this methode when i want to cut of parts of the picture.

When it would be for showing in another way I would use the sizemode of the
picturebox.

Cor
 
P

Peter Proost

I realy want to resize the picture and then save it, because I need the
picture at 128*128

grtz Peter
 
P

Peter Proost

Hi Cor, no I don't need an thumbnail. It's a dynamic drawing program I've
made on which the user can draw and resize a real window by using
parameters, the original drawing is done on a maximum 400*400 bitmap, but
after the drawing is done I need to save it as maximum 128*128 bitmap, for
example if they draw a window which is 1000mm large and 500mm high the
orignal bitmap will be 400*200 but then I need to convert it to a 128*64
bitmap and I thought it would work with the graphics.drawimage and the
correct dimensions but if I use this method the right and bottom line of the
picture get cut off, this happens if you in the code I posted comment out
the line
g2.pixeloffsetmode = drawing2d.pixeloffsetMode.highquality

If I use the
g2.pixeloffsetmode = drawing2d.pixeloffsetMode.highquality
line, it works ok but the the color isn't black anymore, that's why I create
the black(bitmap) method in the code

Grtz Peter & thnx for your help
 
C

Cor Ligthert

Peter,

Thanks because with this post you resolve a problem I once had and now know
that there is an answer.

Cor
 
P

Peter Proost

I'm glad I could help you for once, because normaly it's you helping the
people here, but have you got an idea how I can solve my little problem?
Because I realy would like the image to stay black and white and preferably
not by using my method to go over the entire bitmap pixel by pixel.

thnx Peter
 
P

Peter Proost

Hi larry thnx for your response I know about the white border, but when I do
it like that the image isn't black anymore, thats why I wrote the black
method,

grtz Peter
 
L

Larry Serflaten

Peter Proost said:
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

You haven't really described the problem. However the small image I
saw had a white border at the left an right. That means you must have
made the image too big:

Dim bNew As New Bitmap(CInt(301 / scale) + 1, CInt(301 / scale) + 1)
S/B

Dim bNew As New Bitmap(CInt(301 / scale), CInt(301 / scale))


Then comment out your black line:
black(bNew)
S/B

' black(bNew)


Is that better?
LFS
 
L

Larry Serflaten

Peter Proost said:
I'm glad I could help you for once, because normaly it's you helping the
people here, but have you got an idea how I can solve my little problem?
Because I realy would like the image to stay black and white and preferably
not by using my method to go over the entire bitmap pixel by pixel.

thnx Peter

I think the border would be the least of your worries. Add a couple ellipeses
to the drawing and then try different interpolation modes. Its the drawing you
want to look nice, is it not?

Draw these:

g.DrawRectangle(Pens.Black, 0, 0, 300, 300)
g.DrawEllipse(Pens.Blue, 30, 30, 50, 90)
g.DrawEllipse(Pens.Green, 30, 100, 150, 50)


And try this combination:

g2.Clear(Color.Black)
g2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
g2.DrawImage(PictureBox1.Image, New Rectangle(0, 0, CInt(301 / scale), CInt(301 / scale)), 0, 0,
PictureBox1.Image.Width, PictureBox1.Image.Height, GraphicsUnit.Pixel)


Don't call your Black routine. See if that will work OK....

LFS
 
P

Peter Proost

Hello Larry that works but also makes the black rectangle gray, and I want
it to stay black, if I don't use an interpolationMode it stays black but the
right and bottom border of the square are cut off, and the border is very
important because my program draws the picture of a real window and a window
without a border,frame wouldn't look nice ;-)

thnx for your time and help

PS: first I got an all black square but that was because
g2.clear(color.black) :)

Larry Serflaten said:
I think the border would be the least of your worries. Add a couple ellipeses
to the drawing and then try different interpolation modes. Its the drawing you
want to look nice, is it not?

Draw these:

g.DrawRectangle(Pens.Black, 0, 0, 300, 300)
g.DrawEllipse(Pens.Blue, 30, 30, 50, 90)
g.DrawEllipse(Pens.Green, 30, 100, 150, 50)


And try this combination:

g2.Clear(Color.Black)
g2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
g2.DrawImage(PictureBox1.Image, New Rectangle(0, 0, CInt(301 /
scale), CInt(301 / scale)), 0, 0,
 
P

Peter Proost

Hi just to let you know that I've found a solution that works for me, before
I resize the picture to it's smaller format, I first resize to a bigger
bitmap so that the my original pictures border isn't the border anymore, I
start drawing on the bigger bitmap at point (10,10) and thus the black lines
don't get cut off when I resize the image to a smaller size and the picture
stays black&white because I don't have to use an interpolationMode

Thnx Larry&Cor for your time&help

greetz Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top