Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ghost images on saved image

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
I am drawing basic shapes on a Form.
To draw a line (1)mouse down to start the line (2)with mouse down, drag to lengthen the line (3) mouse up.
Same procedure for rectangle, circles, ellipses,etc.
I am using:
Dim bm As Bitmap
Dim g As Graphics
and
bm = New Bitmap(Me.Width, Me.Height, Me.CreateGraphics)
g = Graphics.FromImage(bm)
and
g.drawline, g.drawrectangle, etc

After completing the drawing, I save the drawing by copying the bitmap to a Picturebox (PB) and save it:
PB.Image = bm
PB.Image.Save("C:\Sketch\Test1.bmp")

The final positions of the shapes are shown properly in the saved picture.
But . . .
The problem is that the saved picture has faint images of the shapes that existed when I was dragging the shapes to the desired size and position.
Somewhere during all this I saw a message about "transparent images".

What is happening?
How can I save the drawing as a .bmp file without these ghosts showing?
Is there a way to save the Form directly to the .bmp file?
 
Post your code please. Maybe someone else will know right off, but I can't think of how I could do that without doing it on purpose so it really depends on how you have it all coded. Posting a picture might be good as well to have a better idea of what you mean by faint images.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I discovered the cause of the 'ghost' lines.
To move a line, for example, each time I dragged one end of the line, the programs first erased the old line by redrawing the line using the Form's Backcolor:
g.DrawLine(penBackColor, .Loc1, .Loc2)
and then drawing the object in the new location:
g.DrawLine(Pens.Blue, .Loc1, .Loc2).

The problem was the penBackColor differed slightly from the actual Form.Backcolor.

They were close enough that the difference could not be seen on the screen. But when the image was saved to a file which was then viewed by another program that displayed it on a White background, the faint lines showed up.

I fixed the problem by ensuring the the 'erase' color is the same as the backcolor.
 
Ah, ok now that would make sense. I couldn't think how you would leave behind only a faint image. Before you posted I had started off and on as I could modifying a signature box I had made as an example for someone else. I'm going to post it since it is done anyway. Here is how I approached it. It uses 2 picture boxes, 3 buttons, and 3 radio buttons.

Code:
Public Class Form1
    Enum ButtonToggle
        Up = 1
        Down = 2
    End Enum

    Enum DrawingBrush
        FreeStyle = 0
        DrawBox = 1
        DrawEllips = 2
    End Enum

    Dim SignatureFile As String = "C:\signature.bmp"
    Dim ButtonState As ButtonToggle = ButtonToggle.Up
    Dim mouseCoord As New Collection
    Dim grp As New Drawing2D.GraphicsPath
    Dim DrawSurface As Graphics
    Dim LastDraw As PictureBox
    Dim DrawBMP As Bitmap
    Dim CurrentDrawBrush As DrawingBrush = DrawingBrush.FreeStyle
    Dim DashPen As System.Drawing.Pen


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NewDrawSurface()
        DashPen = New System.Drawing.Pen(DrawColor_btn.BackColor)
        DashPen.DashStyle = Drawing2D.DashStyle.Dash
    End Sub

    'Create a new surface to draw on
    Private Sub NewDrawSurface()
        'Time stamp the filename
        SignatureFile = SignatureFile.Replace("signature", "signature " & Now.ToString("MM-dd-yy hhmmss"))
        DrawBMP = New Bitmap(Signature_pb.Width, Signature_pb.Height)
        DrawSurface = Graphics.FromImage(DrawBMP)
        DrawSurface.FillRectangle(New SolidBrush(Color.Gray), 0, 0, DrawBMP.Width, DrawBMP.Height)
    End Sub

    Private Sub Signature_pb_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseMove
        If ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.FreeStyle Then
            mouseCoord.Add(e.Location)

            If mouseCoord.Count > 1 Then
                grp.AddLine(mouseCoord.Item(mouseCoord.Count - 1), e.Location)
                Signature_pb.CreateGraphics.DrawPath(New Pen(DrawColor_btn.BackColor), grp)
                DrawSurface.DrawPath(New Pen(DrawColor_btn.BackColor), grp)
            End If
        ElseIf ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.DrawBox Then
            Signature_pb = LastDraw
            Signature_pb.Refresh()
            mouseCoord.Add(e.Location)

            Dim topPoint As System.Drawing.Point = mouseCoord(1)
            Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)

            Dim ngrp As New Drawing2D.GraphicsPath
            ngrp.AddRectangle(boxRec)

            Signature_pb.CreateGraphics.DrawPath(DashPen, ngrp)
        ElseIf ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.DrawEllips Then
            Signature_pb = LastDraw
            Signature_pb.Refresh()
            mouseCoord.Add(e.Location)

            Dim topPoint As System.Drawing.Point = mouseCoord(1)
            Dim ellipsRec As Rectangle = CalculateRectangle(topPoint, e.Location)
           
            Dim ngrp As New Drawing2D.GraphicsPath
            ngrp.AddEllipse(ellipsRec)
            Signature_pb.CreateGraphics.DrawPath(DashPen, ngrp)
        End If

        'x_lbl.Text = e.X
        'Y_lbl.Text = e.Y
        'If mouseCoord.Count > 0 Then lastCoord_lbl.Text = mouseCoord(1).ToString
    End Sub

    Private Sub Signature_pb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseDown
        If ButtonState = ButtonToggle.Up Then LastDraw = Signature_pb
        ButtonState = ButtonToggle.Down
    End Sub

    Private Sub Signature_pb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseUp
        If mouseCoord.Count = 0 And CurrentDrawBrush = DrawingBrush.FreeStyle Then
            'Must dot our i's
            Dim bmp As New Bitmap(1, 1)
            bmp.SetPixel(0, 0, Color.White)
            Signature_pb.CreateGraphics.DrawImage(bmp, e.Location)
            DrawSurface.DrawImage(bmp, e.Location)
        ElseIf CurrentDrawBrush = DrawingBrush.DrawBox And mouseCoord.Count > 0 Then
            Dim topPoint As System.Drawing.Point = mouseCoord(1)
            Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)

            DrawSurface.DrawRectangle(New Pen(DrawColor_btn.BackColor), boxRec)

            'ready for new what ever.
            mouseCoord.Clear()
            grp = New Drawing2D.GraphicsPath
        ElseIf CurrentDrawBrush = DrawingBrush.DrawEllips And mouseCoord.Count > 0 Then
            Dim topPoint As System.Drawing.Point = mouseCoord(1)
            Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)

            DrawSurface.DrawEllipse(New Pen(DrawColor_btn.BackColor), boxRec)

            'ready for new what ever.
            mouseCoord.Clear()
            grp = New Drawing2D.GraphicsPath
        Else
            'ready for new word
            mouseCoord.Clear()
            grp = New Drawing2D.GraphicsPath
        End If

        Signature_pb.Image = DrawBMP
        ButtonState = ButtonToggle.Up
    End Sub

    Private Function CalculateRectangle(ByVal p1 As System.Drawing.Point, ByVal p2 As System.Drawing.Point) As System.Drawing.Rectangle
        Dim TopX As Integer
        Dim TopY As Integer
        Dim xBig As Integer = Math.Max(p1.X, p2.X)
        Dim xSmall As Integer = Math.Min(p1.X, p2.X)
        Dim yBig As Integer = Math.Max(p1.Y, p2.Y)
        Dim ySmall As Integer = Math.Min(p1.Y, p2.Y)
        Dim recWidth As Integer = xBig - xSmall
        Dim recHeight As Integer = yBig - ySmall

        If p1.X > p2.X Then
            TopX = p2.X
        Else
            TopX = p1.X
        End If

        If p1.Y > p2.Y Then
            TopY = p2.Y
        Else
            TopY = p1.Y
        End If

        Return New Rectangle(TopX, TopY, recWidth, recHeight)
    End Function

    Private Sub Clear_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_btn.Click
        Signature_pb.CreateGraphics.Clear(Signature_pb.BackColor)
        NewDrawSurface()
        mouseCoord.Clear()
    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
        PictureBox1.Image = Signature_pb.Image
        Signature_pb.Image.Save(SignatureFile)
    End Sub

    Private Sub free_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles free_btn.Click
        CurrentDrawBrush = DrawingBrush.FreeStyle
    End Sub

    Private Sub box_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles box_btn.Click
        CurrentDrawBrush = DrawingBrush.DrawBox
    End Sub

    Private Sub ellips_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ellips_btn.Click
        CurrentDrawBrush = DrawingBrush.DrawEllips
    End Sub

    Private Sub DrawBtns_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles free_btn.CheckedChanged, box_btn.CheckedChanged, ellips_btn.CheckedChanged
        Dim currentButton As RadioButton = CType(sender, RadioButton)

        If currentButton.Checked = True Then
            currentButton.BackColor = System.Drawing.SystemColors.ButtonShadow
        Else
            currentButton.BackColor = System.Drawing.SystemColors.ButtonFace
        End If
    End Sub

    Private Sub DrawColor_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawColor_btn.Click
        Dim cldlg As New ColorDialog

        If cldlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            DrawColor_btn.BackColor = cldlg.Color
        End If
    End Sub
End Class
Maybe you will find somethine in all of that you can use. You can't drag the box like you said yours could.

Form designer code:
Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Signature_pb = New System.Windows.Forms.PictureBox
        Me.save_btn = New System.Windows.Forms.Button
        Me.Clear_btn = New System.Windows.Forms.Button
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.box_btn = New System.Windows.Forms.RadioButton
        Me.free_btn = New System.Windows.Forms.RadioButton
        Me.Y_lbl = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.x_lbl = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.lastCoord_lbl = New System.Windows.Forms.Label
        Me.ellips_btn = New System.Windows.Forms.RadioButton
        Me.DrawColor_btn = New System.Windows.Forms.Button
        CType(Me.Signature_pb, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Signature_pb
        '
        Me.Signature_pb.BackColor = System.Drawing.SystemColors.ButtonShadow
        Me.Signature_pb.Location = New System.Drawing.Point(372, 226)
        Me.Signature_pb.Name = "Signature_pb"
        Me.Signature_pb.Size = New System.Drawing.Size(326, 153)
        Me.Signature_pb.TabIndex = 0
        Me.Signature_pb.TabStop = False
        '
        'save_btn
        '
        Me.save_btn.Location = New System.Drawing.Point(623, 385)
        Me.save_btn.Name = "save_btn"
        Me.save_btn.Size = New System.Drawing.Size(75, 23)
        Me.save_btn.TabIndex = 1
        Me.save_btn.Text = "Save"
        Me.save_btn.UseVisualStyleBackColor = True
        '
        'Clear_btn
        '
        Me.Clear_btn.Location = New System.Drawing.Point(372, 385)
        Me.Clear_btn.Name = "Clear_btn"
        Me.Clear_btn.Size = New System.Drawing.Size(75, 23)
        Me.Clear_btn.TabIndex = 2
        Me.Clear_btn.Text = "Clear"
        Me.Clear_btn.UseVisualStyleBackColor = True
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.ButtonShadow
        Me.PictureBox1.Location = New System.Drawing.Point(12, 12)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(326, 153)
        Me.PictureBox1.TabIndex = 3
        Me.PictureBox1.TabStop = False
        '
        'box_btn
        '
        Me.box_btn.Appearance = System.Windows.Forms.Appearance.Button
        Me.box_btn.Location = New System.Drawing.Point(291, 255)
        Me.box_btn.Name = "box_btn"
        Me.box_btn.Size = New System.Drawing.Size(75, 23)
        Me.box_btn.TabIndex = 4
        Me.box_btn.Text = "Box"
        Me.box_btn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.box_btn.UseVisualStyleBackColor = True
        '
        'free_btn
        '
        Me.free_btn.Appearance = System.Windows.Forms.Appearance.Button
        Me.free_btn.Checked = True
        Me.free_btn.Location = New System.Drawing.Point(291, 226)
        Me.free_btn.Name = "free_btn"
        Me.free_btn.Size = New System.Drawing.Size(75, 23)
        Me.free_btn.TabIndex = 5
        Me.free_btn.TabStop = True
        Me.free_btn.Text = "FreeStyle"
        Me.free_btn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.free_btn.UseVisualStyleBackColor = True
        '
        'Y_lbl
        '
        Me.Y_lbl.Location = New System.Drawing.Point(659, 428)
        Me.Y_lbl.Name = "Y_lbl"
        Me.Y_lbl.Size = New System.Drawing.Size(39, 13)
        Me.Y_lbl.TabIndex = 7
        Me.Y_lbl.Text = "Label1"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(636, 428)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(17, 13)
        Me.Label2.TabIndex = 8
        Me.Label2.Text = "Y:"
        '
        'x_lbl
        '
        Me.x_lbl.Location = New System.Drawing.Point(582, 428)
        Me.x_lbl.Name = "x_lbl"
        Me.x_lbl.Size = New System.Drawing.Size(39, 13)
        Me.x_lbl.TabIndex = 9
        Me.x_lbl.Text = "Label1"
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(559, 428)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(17, 13)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "X:"
        '
        'lastCoord_lbl
        '
        Me.lastCoord_lbl.AutoSize = True
        Me.lastCoord_lbl.Location = New System.Drawing.Point(369, 428)
        Me.lastCoord_lbl.Name = "lastCoord_lbl"
        Me.lastCoord_lbl.Size = New System.Drawing.Size(55, 13)
        Me.lastCoord_lbl.TabIndex = 11
        Me.lastCoord_lbl.Text = "LastCoord"
        '
        'ellips_btn
        '
        Me.ellips_btn.Appearance = System.Windows.Forms.Appearance.Button
        Me.ellips_btn.Location = New System.Drawing.Point(291, 284)
        Me.ellips_btn.Name = "ellips_btn"
        Me.ellips_btn.Size = New System.Drawing.Size(75, 23)
        Me.ellips_btn.TabIndex = 12
        Me.ellips_btn.Text = "Ellips"
        Me.ellips_btn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.ellips_btn.UseVisualStyleBackColor = True
        '
        'DrawColor_btn
        '
        Me.DrawColor_btn.BackColor = System.Drawing.Color.White
        Me.DrawColor_btn.Location = New System.Drawing.Point(372, 197)
        Me.DrawColor_btn.Name = "DrawColor_btn"
        Me.DrawColor_btn.Size = New System.Drawing.Size(75, 23)
        Me.DrawColor_btn.TabIndex = 13
        Me.DrawColor_btn.Text = "Draw Color"
        Me.DrawColor_btn.UseVisualStyleBackColor = False
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(710, 450)
        Me.Controls.Add(Me.DrawColor_btn)
        Me.Controls.Add(Me.ellips_btn)
        Me.Controls.Add(Me.lastCoord_lbl)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.x_lbl)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Y_lbl)
        Me.Controls.Add(Me.free_btn)
        Me.Controls.Add(Me.box_btn)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Clear_btn)
        Me.Controls.Add(Me.save_btn)
        Me.Controls.Add(Me.Signature_pb)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.Signature_pb, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Signature_pb As System.Windows.Forms.PictureBox
    Friend WithEvents save_btn As System.Windows.Forms.Button
    Friend WithEvents Clear_btn As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents box_btn As System.Windows.Forms.RadioButton
    Friend WithEvents free_btn As System.Windows.Forms.RadioButton
    Friend WithEvents Y_lbl As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents x_lbl As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lastCoord_lbl As System.Windows.Forms.Label
    Friend WithEvents ellips_btn As System.Windows.Forms.RadioButton
    Friend WithEvents DrawColor_btn As System.Windows.Forms.Button

End Class

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I forgot to make a small change when I moved the DashPen.

Code:
    Private Sub DrawColor_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawColor_btn.Click
        Dim cldlg As New ColorDialog

        If cldlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            DrawColor_btn.BackColor = cldlg.Color
            [red]DashPen.Color = cldlg.Color[/red]
        End If
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top