Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用c语言从pictureBox保存图像#_C#_Save_Picturebox_Nullreferenceexception - Fatal编程技术网

C# 用c语言从pictureBox保存图像#

C# 用c语言从pictureBox保存图像#,c#,save,picturebox,nullreferenceexception,C#,Save,Picturebox,Nullreferenceexception,我的程序允许用户在图片框中绘制,这与MS paint的方式类似,现在我正试图将pictureBox保存为.jpg文件,但在尝试这样做时,出现了null异常错误 编辑:应该提到这是一个NullReferenceException 这是我的“保存”按钮,在这里我得到异常错误: private void button3_Click(object sender, EventArgs e) { pictureBox1.Image.Save(@"C:\New folder\pi

我的程序允许用户在图片框中绘制,这与MS paint的方式类似,现在我正试图将pictureBox保存为.jpg文件,但在尝试这样做时,出现了null异常错误

编辑:应该提到这是一个NullReferenceException

这是我的“保存”按钮,在这里我得到异常错误:

   private void button3_Click(object sender, EventArgs e)
    {
        pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
    }
下面是我剩下的代码:

    public Form2()
    {
        InitializeComponent();

        //creates items for combobox brush sizes
        for (int i = 1; i <= 20; i++)
        {
            string[] numbers = { i.ToString() };
            comboBox1.Items.AddRange(numbers);
        }
    }


    bool paint = false;
    SolidBrush color = new SolidBrush(Color.Black);

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        paint = true;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        paint = false;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (paint == true)
        {

            int brushSize = Convert.ToInt32(comboBox1.SelectedItem);
            Graphics g = pictureBox1.CreateGraphics();
            if (comboBox1.SelectedIndex > 0)
            {
                g.FillEllipse(color, e.X, e.Y, brushSize, brushSize);
            }
            else
            {
                g.FillEllipse(color, e.X, e.Y, 10, 10);
            }
            g.Dispose();
        }
    }


    //button that opens colour dialog box
    private void button1_Click_1(object sender, EventArgs e)
    {
        ColorDialog cld = new ColorDialog();

        if (cld.ShowDialog() == DialogResult.OK)
        {
            color = new SolidBrush(cld.Color);
        }
    }

    //Button that clears pictureBox
    private void Button2_Click_1(object sender, EventArgs e)
    {
        Graphics g1 = pictureBox1.CreateGraphics();
        g1.Clear(pictureBox1.BackColor);
    }
public Form2()
{
初始化组件();
//为组合框笔刷大小创建项目
对于(int i=1;i 0)
{
g、 填充椭圆(颜色、e.X、e.Y、笔刷大小、笔刷大小);
}
其他的
{
g、 填充椭圆(颜色,e.X,e.Y,10,10);
}
g、 处置();
}
}
//打开“颜色”对话框的按钮
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
ColorDialog cld=新建ColorDialog();
if(cld.ShowDialog()==DialogResult.OK)
{
颜色=新的SolidBrush(cld.color);
}
}
//清除pictureBox的按钮
私有无效按钮2\u单击\u 1(对象发送者,事件参数e)
{
Graphics g1=pictureBox1.CreateGraphics();
g1.清晰(图片B1.背景色);
}

如果错误为
ArgumentNullException
,请确保您试图保存到的文件夹存在

编辑:

除了下面的评论之外,这个问题很可能是由于PictureBox中没有加载图像造成的


看。

我想你的图片盒没有照片。单击按钮1时,图片框图像为空。右键单击图片框,然后在“属性”中导入一些照片,然后再运行代码。

您应该通过相应的
图形对象绘制图像上的所有内容。以下是我为您更正的代码,它至少比您的代码更好、更简洁:

 public Form2() {
    InitializeComponent();
    //creates items for combobox brush sizes
    for (int i = 1; i <= 20; i++)
    {
        string[] numbers = { i.ToString() };
        comboBox1.Items.AddRange(numbers);
    }
    //initialize a blank image for your PictureBox
    pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    g = Graphics.FromImage(pictureBox1.Image);
 }
 Graphics g;
SolidBrush color = new SolidBrush(Color.Black);

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        int brushSize = comboBox1.SelectedIndex > 0 ?
                        Convert.ToInt32(comboBox1.SelectedItem) : 10;
        g.FillEllipse(color, e.X, e.Y, brushSize, brushSize);
        pictureBox1.Invalidate();//This is important to re-draw the updated Image
    }
}
//button that opens colour dialog box
private void button1_Click_1(object sender, EventArgs e) {
    ColorDialog cld = new ColorDialog();
    if (cld.ShowDialog() == DialogResult.OK) {
        color = new SolidBrush(cld.Color);
    }
}
//Button that clears pictureBox
private void Button2_Click_1(object sender, EventArgs e) {
    g.Clear(pictureBox1.BackColor);
}
private void button3_Click(object sender, EventArgs e) {
    pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
}
public Form2(){
初始化组件();
//为组合框笔刷大小创建项目
对于(int i=1;i 0?
Convert.ToInt32(组合框1.SelectedItem):10;
g、 填充椭圆(颜色、e.X、e.Y、笔刷大小、笔刷大小);
pictureBox1.Invalidate();//这对于重新绘制更新的图像很重要
}
}
//打开“颜色”对话框的按钮
私有无效按钮1\u单击\u 1(对象发送者,事件参数e){
ColorDialog cld=新建ColorDialog();
if(cld.ShowDialog()==DialogResult.OK){
颜色=新的SolidBrush(cld.color);
}
}
//清除pictureBox的按钮
私有无效按钮2\u单击\u 1(对象发送者,事件参数e){
g、 清晰(图片B1.背景色);
}
私有无效按钮3\u单击(对象发送者,事件参数e){
pictureBox1.Image.Save(@“C:\newfolder\picture.jpg”,ImageFormat.Jpeg);
}
如图所示,重新创建
图形
对象可能会导致闪烁。此外,您必须使用
从图像
创建位图,否则您的图形将不会显示在图像上,并且无论您在屏幕上看到什么,它都将保持为空

//declare graphics globally
Graphics g; 

private void Form_Load(object sender, EventArgs e)
{

    picCanvas.Image = new Bitmap(picCanvas.Width, picCanvas.Height);

    // create the graphics object here and not in DrawLine, which 
    // may cause flicker each time its instantiated

    graphics = Graphics.FromImage(picCanvas.Image);

    DrawLine();

}

private void DrawLine()
{

    //Do not recreate the Graphics object here. 
    //Recreating it seems to 'erase' the existing image
    //Which causes flicker that double-buffering can't manage

    System.Drawing.Pen pen;
    pen.Color = Color.Black;

    // If you create the graphics object from the bitmap, this
    // should paint to the bitmap, so the Image object won't be null
    g.DrawLine(1, 1, picCanvas.Width - 2, picCanvas.Height - 2);   

}

您确定在您尝试保存图像之前不会出现
按钮\u Click\u 1
。@Aviral第一个按钮的单击效果如何?您从未实际分配PictureBox.image属性,而是选择直接绘制。这很好。但是,当您尝试保存图像时,该属性仍然为空t、 您好,我想我应该指定这是一个NullReferenceException您好,我在属性下添加了一个白色背景,消除了null错误,但即使在我绘制它时,它也会保存一个空白的白色图像。您可能知道我可以用于解决此问题的任何好的源代码或有用的代码吗?@meau94您好,我认为此链接有助于解决您的问题lem您好,我非常感谢您花时间修复我的代码:)它似乎几乎可以完美地工作。它确实保存了图形,但出于某种原因,它的背景是黑色的。