c# picturebox中的图片怎么保存
画图片的代码:stringthisimg=((ListBox)sender).SelectedItem.ToString();stringname=txtfolderpa...
画图片的代码 :
string thisimg = ((ListBox)sender).SelectedItem.ToString();
string name = txtfolderpath.Text + "\\" + thisimg;
if (File.Exists(name))
{
Bitmap map = new Bitmap(name);
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(map, 0, 0, pictureBox1.Width, pictureBox1.Height);
map.Dispose();
//Bitmap bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//Graphics g = Graphics.FromImage(bt);
//g.FillRectangle(new SolidBrush(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height);
}
存图片的代码:
saveFileDialog1.InitialDirectory = "c:";
saveFileDialog1.Filter = "Png图像(*.png)|*.png|所有格式(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string folderP = saveFileDialog1.FileName;
Image img = pictureBox1.Image;
img.Save(folderP);
}
PS:因为有删除图片的功能,所以不能是直接绑定图片:
//Bitmap bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//Graphics g = Graphics.FromImage(bt);
//g.FillRectangle(new SolidBrush(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height);
所以上面新建了一个graphics,然后在画布上画了,但是,发现这样做到保存图片的时候,就会出现pictureBox1.Image是null的问题,刚刚网上搜了一下,知道了graphics的一切操作是针对pictureBox1这个控件外表的修改,而不是Image属性,求解决方法~ 展开
string thisimg = ((ListBox)sender).SelectedItem.ToString();
string name = txtfolderpath.Text + "\\" + thisimg;
if (File.Exists(name))
{
Bitmap map = new Bitmap(name);
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(map, 0, 0, pictureBox1.Width, pictureBox1.Height);
map.Dispose();
//Bitmap bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//Graphics g = Graphics.FromImage(bt);
//g.FillRectangle(new SolidBrush(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height);
}
存图片的代码:
saveFileDialog1.InitialDirectory = "c:";
saveFileDialog1.Filter = "Png图像(*.png)|*.png|所有格式(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string folderP = saveFileDialog1.FileName;
Image img = pictureBox1.Image;
img.Save(folderP);
}
PS:因为有删除图片的功能,所以不能是直接绑定图片:
//Bitmap bt = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//Graphics g = Graphics.FromImage(bt);
//g.FillRectangle(new SolidBrush(Color.Black), 0, 0, pictureBox1.Width, pictureBox1.Height);
所以上面新建了一个graphics,然后在画布上画了,但是,发现这样做到保存图片的时候,就会出现pictureBox1.Image是null的问题,刚刚网上搜了一下,知道了graphics的一切操作是针对pictureBox1这个控件外表的修改,而不是Image属性,求解决方法~ 展开
2013-07-18
展开全部
很简单,定义一个全局变量:public Bitmap pbmap;
然后所有的图都画在这个bitmap上面,保存时只要将pbmap保存就行了
pbmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
if (listBox1.Items.Count <= 0 || listBox1.SelectedItem == null)
{
return;
}
string thisimg = ((ListBox)sender).SelectedItem.ToString();
string name = txtfolderpath.Text + "\\" + thisimg;
if (File.Exists(name))
{
Bitmap map = new Bitmap(name);
Graphics g = Graphics.FromImage(pbmap);
g.DrawImage(map, 0, 0, pictureBox1.Width, pictureBox1.Height);
map.Dispose();
pictureBox1.Image = pbmap;
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string folderP = saveFileDialog1.FileName;
Image img = pbmap;
img.Save(folderP);
}
然后所有的图都画在这个bitmap上面,保存时只要将pbmap保存就行了
pbmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
if (listBox1.Items.Count <= 0 || listBox1.SelectedItem == null)
{
return;
}
string thisimg = ((ListBox)sender).SelectedItem.ToString();
string name = txtfolderpath.Text + "\\" + thisimg;
if (File.Exists(name))
{
Bitmap map = new Bitmap(name);
Graphics g = Graphics.FromImage(pbmap);
g.DrawImage(map, 0, 0, pictureBox1.Width, pictureBox1.Height);
map.Dispose();
pictureBox1.Image = pbmap;
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string folderP = saveFileDialog1.FileName;
Image img = pbmap;
img.Save(folderP);
}
展开全部
调用pictureBox的DrawToBitmap方法就可以了:
Bitmap bmp = new Bitmap(pictureBox1.BackgroundImage);
bmp.Save("c:\\1.bmp");
Bitmap bmp = new Bitmap(pictureBox1.BackgroundImage);
bmp.Save("c:\\1.bmp");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
pictureBox1.Image = map 就行了呀。DrawImage 多余了。需要缩放的话,设置pictureBox的缩放模式就好了。
而且从你的代码来看copy 图像文件就好了。也用不着通过picutrebox来中转一下吧。
而且从你的代码来看copy 图像文件就好了。也用不着通过picutrebox来中转一下吧。
追问
可以加您QQ问吗?我有删除功能,所以不能占用图片文件,要dispose掉,用pictureBox1.Image = map显示出来是错误,还有水印功能,所以需要picturebox中转一下
我的QQ:939676215
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接保存图片 map.Save("fileName");
追问
map DISPOSE掉了,而且不是在同一个函数中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询