保存PictureBox中图片

shadow_2006 2009-06-25 11:23:58
pictureBox1.ImageLocation = "http://csdnimg.cn/bbs/t/5/i/pic_logo.gif";
如何pictureBox1中显示的图片保存为本地文件?
这个时候pictureBox1.Image为空,所以Image.Save()不行。
各位大哥帮帮忙!
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-06-26
  • 打赏
  • 举报
回复
1 直接保存原始图形
pictureBox1.ImageLocation = "http://www.baidu.com/img/baidu_logo.gif"; System.Net.WebClient _Client = new System.Net.WebClient();
_Client.DownloadFile(pictureBox1.ImageLocation, @"C:\1.gif");

2 使用PictureBox1.DrawToBitmap
ghost5216 2009-06-26
  • 打赏
  • 举报
回复
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDesDC, int x, int y, int w, int h,
HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);



private void Form1_Load(object sender, EventArgs e)
{


pictureBox1.ImageLocation = "http://csdnimg.cn/bbs/t/5/i/pic_logo.gif";
}

public static Bitmap FromGraphics(Graphics g, int w, int h)
{
const int SRCCOPY = 0xcc0020;
Bitmap b = new Bitmap(w, h);
Graphics gd = Graphics.FromImage(b);

BitBlt(new HandleRef(null, gd.GetHdc()), 0, 0, w, h,
new HandleRef(null, g.GetHdc()), 0, 0, SRCCOPY);
gd.ReleaseHdc(); g.ReleaseHdc();
return b;
}


private void button1_Click(object sender, EventArgs e)
{

FromGraphics(pictureBox1.CreateGraphics(), pictureBox1.Width, pictureBox1.Height).Save("D:/a.jpg");
}


注意
pictureBox1.ImageLocation = "http://csdnimg.cn/bbs/t/5/i/pic_logo.gif";

FromGraphics(pictureBox1.CreateGraphics(), pictureBox1.Width, pictureBox1.Height).Save("D:/a.jpg");
不能连续放在一起使用
因为使用CreateGraphics 所以请确定pictureBox1已经显示后 再调用FromGraphics

shadow_2006 2009-06-26
  • 打赏
  • 举报
回复
问题解决!结贴给分!
shadow_2006 2009-06-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ChrisAK 的回复:]

Image.FromFile("http://csdnimg.cn/bbs/t/5/i/pic_logo.gif").Save()试试.
[/Quote]
不支持URL.....
ChrisAK 2009-06-25
  • 打赏
  • 举报
回复

Image.FromFile("http://csdnimg.cn/bbs/t/5/i/pic_logo.gif").Save()试试.

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧