Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
Questionpassword creation in c# winform Pin
Member 1002123530-Apr-13 18:39
Member 1002123530-Apr-13 18:39 
AnswerRe: password creation in c# winform Pin
Richard MacCutchan30-Apr-13 21:42
mveRichard MacCutchan30-Apr-13 21:42 
AnswerRe: password creation in c# winform Pin
GuyThiebaut1-May-13 2:14
professionalGuyThiebaut1-May-13 2:14 
Questionhow to get the value of a control in a process which start with Process.start Pin
Edward_Zhao30-Apr-13 18:11
professionalEdward_Zhao30-Apr-13 18:11 
AnswerRe: how to get the value of a control in a process which start with Process.start Pin
Richard MacCutchan30-Apr-13 21:37
mveRichard MacCutchan30-Apr-13 21:37 
GeneralRe: how to get the value of a control in a process which start with Process.start Pin
Edward_Zhao1-May-13 14:33
professionalEdward_Zhao1-May-13 14:33 
GeneralRe: how to get the value of a control in a process which start with Process.start Pin
Richard MacCutchan1-May-13 21:02
mveRichard MacCutchan1-May-13 21:02 
QuestionSaving DrawLine to Image: SOLVED Pin
Member 792126830-Apr-13 3:34
Member 792126830-Apr-13 3:34 
Hi all,
I'm creating an application that will allow me to draw lines with the mouse and then save them to a file in a graphical format. By dint of trying and trying I have come to the code.

The last problem that I can not solve is that I can only save the 'background' of the image but not the drawed shape.

The program consists of a PictureBox1 and a button1, drawing with the left mouse button, closes the shape with the right button and save the.

Thanks in advance to those who want to give me a tip.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Bitmap bm;
        Graphics g;
        GraphicsPath path = new GraphicsPath();

        Pen redPen;
        Pen blackPen1;
        Pen blackPen2;
        Pen orangePen;

        public Form1()
        {
            InitializeComponent();

            g = pictureBox1.CreateGraphics();

            redPen = new Pen(Color.Red, 2);
            blackPen1 = new Pen(Color.Black, 1);
            blackPen2 = new Pen(Color.Black, 2);
            orangePen = new Pen(Color.Orange, 1);

            bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            Shown += Form1_Shown;
        }

        ///////////////////////
        private void Form1_Shown(Object sender, EventArgs e)
        {
        }

        private List<Point> polygonPoints = new List<Point>();

        ///////////////////////
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    polygonPoints.Add(new Point(e.X, e.Y));

                    if (polygonPoints.Count > 1)
                    {
                        path.AddLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]);
                        g.DrawPath(redPen, path);
                    }
                    break;

                case MouseButtons.Right:
                    if (polygonPoints.Count > 2)
                    {
                        path.CloseAllFigures();

                        g.DrawPath(redPen, path);
                        g.FillPath(new SolidBrush(Color.Orange), path);
                    }
                    break;
            }
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromImage(bm);

            // Draw grid
            for (int i = 0; i < pictureBox1.Width; i += 10) g.DrawLine(blackPen1, i, pictureBox1.Height, i, 0);
            for (int i = 0; i < pictureBox1.Height; i += 10) g.DrawLine(blackPen1, 0, i, pictureBox1.Width, i);
            g.DrawLine(blackPen2, 0, pictureBox1.Height - 90, pictureBox1.Width, pictureBox1.Height - 90);
            
            //g.Dispose();

            e.Graphics.DrawImageUnscaled(bm, 0, 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bm.Save("c:\\test.png", ImageFormat.Png);
        }
    }
}

AnswerRe: Saving DrawLine to Image Pin
Eddy Vluggen30-Apr-13 4:05
professionalEddy Vluggen30-Apr-13 4:05 
GeneralRe: Saving DrawLine to Image Pin
Member 792126830-Apr-13 5:11
Member 792126830-Apr-13 5:11 
AnswerRe: Saving DrawLine to Image Pin
Kenneth Haugland30-Apr-13 4:42
mvaKenneth Haugland30-Apr-13 4:42 
GeneralRe: Saving DrawLine to Image Pin
Member 79212681-May-13 0:46
Member 79212681-May-13 0:46 
AnswerRe: Saving DrawLine to Image: SOLVED Pin
Member 79212681-May-13 4:39
Member 79212681-May-13 4:39 
QuestionThe performance of LINQ vs. traditional iteration Pin
Rob Philpott30-Apr-13 3:30
Rob Philpott30-Apr-13 3:30 
AnswerRe: The performance of LINQ vs. traditional iteration Pin
Eddy Vluggen30-Apr-13 4:01
professionalEddy Vluggen30-Apr-13 4:01 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Rob Philpott30-Apr-13 4:31
Rob Philpott30-Apr-13 4:31 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
harold aptroot30-Apr-13 8:33
harold aptroot30-Apr-13 8:33 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Rob Philpott30-Apr-13 8:44
Rob Philpott30-Apr-13 8:44 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
harold aptroot30-Apr-13 8:54
harold aptroot30-Apr-13 8:54 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Eddy Vluggen30-Apr-13 9:42
professionalEddy Vluggen30-Apr-13 9:42 
AnswerRe: The performance of LINQ vs. traditional iteration Pin
Simon_Whale30-Apr-13 4:03
Simon_Whale30-Apr-13 4:03 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Rob Philpott30-Apr-13 4:24
Rob Philpott30-Apr-13 4:24 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Simon_Whale30-Apr-13 4:46
Simon_Whale30-Apr-13 4:46 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Rob Philpott30-Apr-13 4:57
Rob Philpott30-Apr-13 4:57 
GeneralRe: The performance of LINQ vs. traditional iteration Pin
Simon_Whale30-Apr-13 5:32
Simon_Whale30-Apr-13 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.