C#MVS2010画线&面板上的图片框,同时保持图像分离

时间:2014-04-15 17:25:55

标签: c# visual-studio-2010 paint

随意使用您自己程序的任何代码,任何有关以PDF格式保存的问题都将得到解答

我已经有一段时间遇到了问题,我想做的是以下几点:

1)可以上传和成像并显示该图像的程序 - >使用代码

2)在此图像的顶部绘制线条,同时可以删除它们或稍后更改此行的2个点中的1个 - >仍然要做,计划通过检查1)makelines var = false来执行此操作2)onclick鼠标位置靠近线点,如果是这样的话,将鼠标移动到线点,同时按住鼠标并将鼠标设置为鼠标x&收率

3)我想点击左边的图标,然后在上传的图片上绘制 - >工作

4)问题! - 当我在这个图像上绘制时,它会闪烁,以前绘制的线条和图标也是如此,那就是我将图像设置为面板背景,这样我就可以使用面板的绘制方法绘制它,并且我已经成功存储了线条和图标分为数组,而不是每次完成一条线或图标后,这将更新显示图像,最重要的是绘制的线条和图标组成阵列。它闪烁很多的事实是因为我已经使用Mousemove函数来查看将绘制线的位置以及图标的放置位置,并且为此我必须在mousemove上更新它但是这会导致所有线条,图标和背景,以便在我移动的时候像疯了一样轻弹。

这是我的代码,非常感谢任何可以帮助我正确指导的人//修复此问题或尝试。

这是一个视频,清楚地说明发生了什么和问题。

如果代码不清楚,如果有任何问题,请向我道歉。这是我父亲的工作项目,我通常会设计游戏,如果我犯了任何错误告诉我,我是C#的新手!将不胜感激。

亲切的问候Jules Broeks 游戏技术学生 荷兰

视频:http://youtu.be/bL7a-Lewp4Q

如果您需要查看我的数组课程,请告诉我。

代码:

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.Media;
using System.Net;
using Microsoft.VisualBasic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace JulesIsDeBeste
{
    public partial class Form1 : Form
    {
        Bitmap newBitmap;
        System.Drawing.Image file;
        System.Drawing.Image image;
        System.Drawing.Image symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8, symbol9, symbol10, symbol11, symbol12, symbol13, symbol14, symbol15;
        System.Drawing.Image symbol31, symbol32, symbol33, symbol34, symbol35, symbol36, symbol37, symbol38, symbol39, symbol40, symbol41, symbol42, symbol43, symbol44, symbol45;
        System.Drawing.Image symbol46, symbol47, symbol48, symbol49, symbol50, symbol51, symbol52, symbol53, symbol54, symbol55, symbol56, symbol57, symbol58, symbol59, symbol60;
        System.Drawing.Image symbol61, symbol62, symbol63;

        private bool mouse_down = false;
        private Color col = Color.Black;
        private bool makeLine = false;
        int penwidth = 1;
        Pen blackPen;
        Bitmap bitmap;
        int x1;
        int y1;
        int x2;
        int y2;
        LineClass array = new LineClass();
        DrawIcon icon = new DrawIcon();
        System.Drawing.Image selectedIcon;
        private bool drawicon = false;


        public Form1()
        {
            symbol1 = JulesIsDeBeste.Properties.Resources.dice;
            symbol2 = JulesIsDeBeste.Properties.Resources.das;
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                try
                {
                    file = System.Drawing.Image.FromFile(openFileDialog1.FileName);
                    newBitmap = new Bitmap(openFileDialog1.FileName);
                    pictureBox1.Image = file;
                    pictureBox1.Height = panel1.Height;
                    pictureBox1.Width = panel1.Width;
                    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                    panel1.Height = file.Height;
                    panel1.Width = file.Width;
                    panel1.BackgroundImage = file;
                    bitmap = new Bitmap(panel1.Width, panel1.Height);
                    panel1.DrawToBitmap(bitmap, new System.Drawing.Rectangle(panel1.ClientSize.Width - panel1.Width, panel1.ClientSize.Height - panel1.Height, panel1.Width, panel1.Height));
                    pictureBox1.Hide();


                }

                catch (Exception)
                {
                    MessageBox.Show("Verkeerde format, kies een Image file", " -->  Error ! <-- ");

                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

            String filename = Microsoft.VisualBasic.Interaction.InputBox("Opslaan als : ", "Wat moet je toch zonder Jules !", "", 250, 250);
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            //Choose the default start up folder
            string selectedFolder = @"C:\Dev";

            //Set that into the dialog
            folderBrowserDialog1.SelectedPath = selectedFolder;

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                //Grab the folder that was chosen
                selectedFolder = folderBrowserDialog1.SelectedPath;

                // The user selected a folder and pressed the OK button.
                // We print the number of files found.                
                string[] files = Directory.GetFiles(selectedFolder);

            }

            String abc = filename + ".pdf";
            panel1.DrawToBitmap(bitmap, new System.Drawing.Rectangle(panel1.ClientSize.Width - panel1.Width, panel1.ClientSize.Height - panel1.Height, panel1.Width, panel1.Height));
            image = bitmap;
            try
            {

                Document doc = new Document();
                doc.SetPageSize(new iTextSharp.text.Rectangle((panel1.ClientSize.Width - panel1.Width), panel1.ClientSize.Height - panel1.Height, panel1.Width, panel1.Height, 0));
                PdfWriter.GetInstance(doc, new FileStream(selectedFolder + "/" + abc, FileMode.Create));
                doc.Open();
                iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
                doc.Add(pdfImage);
                doc.Close();
                MessageBox.Show("Succesfully Saved At " + selectedFolder);

            }

            catch (Exception)
            {
                //Log error;
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            x1 = e.X;
            y1 = e.Y;
            mouse_down = true;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {

            mouse_down = false;
            if (makeLine)
            {
                array.addToArray(x1, y1, x2, y2, col, penwidth);

            }
            if (drawicon)
            {

                icon.addToArray(x2, y2, selectedIcon);
                panel1.Refresh();
            }


        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {

            toolStripStatusLabel1.Text = e.X + "," + e.Y;
            if (mouse_down == true)
            {

                try
                {

                    x2 = e.X;
                    y2 = e.Y;

                    panel1.Refresh();
                }
                catch (Exception)
                { }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            col = colorDialog1.Color;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (makeLine && !drawicon)
            {

                try
                {
                         blackPen = new Pen(col, penwidth);
                        e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);

                        for (int i = 0; array.linesCount() > i; i++)
                        {
                            blackPen = new Pen(array.getColor(i), array.getPenWidth(i));
                            e.Graphics.DrawLine(blackPen, array.getPointsX1(i), array.getPointsY1(i), array.getPointsX2(i), array.getPointsY2(i));
                        }
                        for (int i = 0; icon.iconCount() > i; i++)
                        {
                            e.Graphics.DrawImage(icon.getIcon(i), icon.getX(i), icon.getY(i));
                        }
                        blackPen = new Pen(col, penwidth);
                }
                catch
                { }
            }

            if (!makeLine && drawicon)
            {
                try
                {
                    e.Graphics.DrawImage(selectedIcon, x2, y2);


                    for (int i = 0; array.linesCount() > i; i++)
                    {
                        blackPen = new Pen(array.getColor(i), array.getPenWidth(i));
                        e.Graphics.DrawLine(blackPen, array.getPointsX1(i), array.getPointsY1(i), array.getPointsX2(i), array.getPointsY2(i));
                    }
                    for (int i = 0; icon.iconCount() > i; i++)
                    {
                        e.Graphics.DrawImage(icon.getIcon(i), icon.getX(i), icon.getY(i));
                    }
                }
                catch
                { }
            }
            if (!makeLine && !drawicon)
            { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!makeLine)
            {
                makeLine = true;
                drawicon = false;
                pictureBox2.BackColor = Color.Honeydew;
                button2.ForeColor = Color.Red;

            }
            else
            {
                makeLine = false;
                button2.ForeColor = Color.Black;
            }

        }


        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            penwidth = trackBar1.Value + 1;
        }


        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            splitContainer1.Panel1MinSize = 250;
            splitContainer1.Panel1.AutoScroll = true;

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            selectedIcon = symbol1;
            if (!drawicon)
            {
                drawicon = true;
                makeLine = false;
                button2.ForeColor = Color.Black;
                pictureBox2.BackColor = Color.Aquamarine;
            }
            else
            {
                drawicon = false;
                pictureBox2.BackColor = Color.Honeydew;
            }
        }

    }
}

0 个答案:

没有答案