C#如何将不同的顺序图像保存到不同的文件名 [英] C# How do I save different sequential images to different filenames

查看:72
本文介绍了C#如何将不同的顺序图像保存到不同的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个适用于Visual C#express 2010的工作代码,该代码可以记录屏幕,效果很好,但是我无法确定每次单击保存时如何将其保存到其他文件名.


这是完整的代码:

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;

命名空间 WindowsFormsApplication1
{
    公共 部分  class  Form1:表单
    {
        公共 Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            
 
        }

        私有位图Get_screen()
        {
            大小s = Screen.PrimaryScreen.Bounds.Size;
            位图bt = 位图(s.Width,s.Height);
            图形g = Graphics.FromImage(bt);
            g.CopyFromScreen( 0  0  0  0 ,s);
            返回 bt;
        }

        私有 无效 timer1_Tick(对象发​​件人,EventArgs e)
        {
            pictureBox1.Image = Get_screen();
        }

        私有 无效 button1_Click(对象发​​件人,EventArgs e)
        {
            timer1.Enabled =  true ;
        }

        私有 无效 button2_Click(对象发​​件人,EventArgs e)
        {
        timer1.Enabled =  false ;
        }

        公共 无效 button3_Click(对象发​​件人,EventArgs e)
        {
                位图c = 位图(pictureBox1.Image);
                c.Save(" ,System.Drawing.Imaging.ImageFormat. Gif);
        }

           

        }
                      
} 



我需要将每个图像保存为image1,image2等,因为我计划将图像转换为avi.被点击.
然后在c.Save命令中将该索引添加到您的文件名中.

 c.Save("  + index. ToString()+ " ,系统....)


此代码可以解决问题:

 公共  int  B =  1 ;
        
私有 无效 button3_Click(对象发​​件人,EventArgs e)

{
位图c = 位图(pictureBox1.Image);
c.Save("  + B.ToString()+ " ,System.Drawing.Imaging.ImageFormat.Gif);
B ++;
} 


使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间WindowsFormsApplication1
{
公共局部类Form1:Form
{

//计数器
private int counter = 0;


公共Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;


}

私有位图Get_screen()
{
大小s = Screen.PrimaryScreen.Bounds.Size;
位图bt =新位图(s.Width,s.Height);
图形g = Graphics.FromImage(bt);
g.CopyFromScreen(0,0,0,0,s);
返回bt;
}

private void timer1_Tick(对象发送者,EventArgs e)
{
pictureBox1.Image = Get_screen();

//在此处增加计数器
计数器++;
}

private void button1_Click(对象发送者,EventArgs e)
{
timer1.Enabled = true;
}

private void button2_Click(对象发送者,EventArgs e)
{
timer1.Enabled = false;
}

public void button3_Click(对象发送者,EventArgs e)
{
位图c =新位图(pictureBox1.Image);
//c.Save("C:\\image.gif,System.Drawing.Imaging.ImageFormat.Gif);
c.Save("C:\\ image" + counter.ToString()+".gif",System.Drawing.Imaging.ImageFormat.Gif);

}

}
}


如果您打算将已保存的图像转换为avi文件,我认为您在每次点击中保存的图像帧
就平滑的avi文件而言,在图像数量方面还远远不够.如果您将代码移至
内 "button3_Click"到根据预设时间触发的事件,例如您的timer1_Tick,您
可以获得很好的结果.

您可以在这里找到有用且方便的保存图像的东西:
http://www.artuxsoft.com


Hi
I have a working code for Visual C# express 2010, that records screen, it works great, but I cant work out how to get it to save to a different filename everytime I click save.


This is the full code:

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            
 
        }

        private Bitmap Get_screen()
        {
            Size s = Screen.PrimaryScreen.Bounds.Size;
            Bitmap bt = new Bitmap(s.Width, s.Height);
            Graphics g = Graphics.FromImage(bt);
            g.CopyFromScreen(0, 0, 0, 0, s);
            return bt;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Image=Get_screen();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
        timer1.Enabled = false;
        }

        public void button3_Click(object sender, EventArgs e)
        {               
                Bitmap c = new Bitmap(pictureBox1.Image);
                c.Save("C:\\image.gif", System.Drawing.Imaging.ImageFormat.Gif);
        }

           

        }
                      
}



I need to save each image as image1, image2, etc because I plan to convert the images to avi.

解决方案

Create an integer index that gets incremented every time the button is clicked.
Then add that index onto your file name in the c.Save command.

c.Save("C:\\image"+index.ToString()+".gif", System.....)


This code sorts out problem:

public int B =1;
        
private void button3_Click(object sender, EventArgs e)

{
Bitmap c = new Bitmap(pictureBox1.Image);
c.Save("C:\\image" + B.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
B ++;
} 


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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

// counter
private int counter=0;


public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;


}

private Bitmap Get_screen()
{
Size s = Screen.PrimaryScreen.Bounds.Size;
Bitmap bt = new Bitmap(s.Width, s.Height);
Graphics g = Graphics.FromImage(bt);
g.CopyFromScreen(0, 0, 0, 0, s);
return bt;
}

private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image=Get_screen();

// increase counter here
counter++;
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}

public void button3_Click(object sender, EventArgs e)
{
Bitmap c = new Bitmap(pictureBox1.Image);
//c.Save("C:\\image.gif", System.Drawing.Imaging.ImageFormat.Gif);
c.Save("C:\\image" + counter.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);

}

}
}


If you are planing to convert the saved images to avi file, I think the image frames you saved in each click
is not sufficient in terms of numbers of the images for a smooth avi files. If you move the code inside the
"button3_Click" to an event which fires based on pre-setting times, like your timer1_Tick, you
can get good results.

You can find useful and convinient save image stuffs here:
http://www.artuxsoft.com


这篇关于C#如何将不同的顺序图像保存到不同的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆