我只想用C#写exe应用程序,想画个图上去,不果,请教。
source:
---------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.Serialization;
using System.IO;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        //The graphic I will be drawed.
        Graphics myGraphic;        //Construct method
        public Form1()
        {
            Image myImage;
            myGraphic = this.CreateGraphics();            InitializeComponent();
            System.Drawing.Pen myPen;
            myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            formGraphics.DrawLine(myPen, 0, 0, 200, 200);
            myPen.Dispose();
            formGraphics.Dispose();
/*
            this.Text = "Image Shower";
            myImage = this.LoadImage();
            if (myImage == null)
                MessageBox.Show("Can not get Image file.");
            else
                this.ShowImage(myImage);
            this.ReleaseGrap();
            //this.Update();
 */
        }        //Load the image from res.
        private Image LoadImage()
        {
            Image myImage = null;
            String filePath = "..\\..\\Resources\\boxer.bmp";            if (File.Exists(filePath)) this.Text = "Find image file";
            FileStream imageFileStream = new FileStream(filePath, FileMode.Open);
            myImage = new Bitmap(imageFileStream);
            return myImage;
        }        //Show the image on form1
        private void ShowImage(Image img)
        {
            myGraphic.DrawImage(img, 0, 0);
            Pen myPen = new Pen(System.Drawing.Color.Red);
            myGraphic.DrawLine(myPen, 0, 0, 20, 20);
            myPen.Dispose();
            //myGraphic.DrawImage(img, this.ClientRectangle, new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); 
        }        //Release the image.
        private void ReleaseGrap()
        {
            myGraphic.Dispose();
        }
    }
}

解决方案 »

  1.   

    '绘画填充颜色
        Private Sub pbFillColor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbFillColor.Paint
            Dim t_rectF As New RectangleF(0, 0, Me.pbFillColor.Width, Me.pbFillColor.Height)
            If Me.m_FillColorTransparency <> 0 Then
                e.Graphics.FillRectangle(New SolidBrush(m_FillColor), t_rectF)
            Else
                '如果是透明的话,用白色代替
                e.Graphics.FillRectangle(New SolidBrush(Color.White), t_rectF)
            End If
        End Sub    '绘画边框颜色
        Private Sub pbLineColor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbLineColor.Paint
            Dim t_rectF As New RectangleF(0, 0, Me.pbLineColor.Width, Me.pbLineColor.Height)
            If Me.m_LineTransparency <> 0 Then
                e.Graphics.FillRectangle(New SolidBrush(m_LineColor), t_rectF)
            Else
                '如果是透明的话,用白色代替
                e.Graphics.FillRectangle(New SolidBrush(Color.White), t_rectF)
            End If
        End Sub    '绘画预览图片
        Private Sub pbPreview_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbPreview.Paint
            Dim t_rect As New Rectangle(10, 10, Me.pbPreview.Width - 20, pbPreview.Height - 20)
            Dim t_rectF As New RectangleF(10, 10, Me.pbPreview.Width - 20, pbPreview.Height - 20)
            If Me.m_FillColorTransparency <> 0 Then
                e.Graphics.FillRectangle(New SolidBrush(m_FillColor), t_rectF)
            Else
                '如果是透明的话,用白色代替
                e.Graphics.FillRectangle(New SolidBrush(Color.White), t_rectF)
            End If        '透明的和宽度为0的不绘画
            If Me.m_LineWidth <> 0 And Me.m_LineTransparency <> 0 Then
                e.Graphics.DrawRectangle(New Pen(Me.m_LineColor, Me.m_LineWidth), t_rect)
            End If
        End Sub这是3个在pictureBox中画图形的函数,如果在Form中画,则写在form的Paint事件中,原理和写法都一样,只是画图形的地方不一样而已. C#和vb.net的原理一样,不再另写代码了...
      

  2.   

    窗体是经常刷新的,每次刷新时都必须重新绘画你自定义要绘画的图形,你原来的代码里的确画了东东了,但当窗体Show出来的时候,整个窗体进行刷新,你没有在Paint 的处理函数中写绘画代码,自然就没有重画你的东东拉
      

  3.   

    把代码放在Form1_OnPaint窗体的Paint事件里