C#怎么让一个程序的界面的图片下保存下来
在线等,回答满意再加分,急我现在通过sampleapplication可以启动CyDisplayEx这个程序,我想在sampleapplication这个界面上设置一个b...
在线等,回答满意再加分,急
我现在通过sampleapplication可以启动CyDisplayEx这个程序,我想在sampleapplication这个界面上设置一个button,通过button可以保存CyDisplayEx界面上的图片,请问这个button里该给出什么样的方法啊? 展开
我现在通过sampleapplication可以启动CyDisplayEx这个程序,我想在sampleapplication这个界面上设置一个button,通过button可以保存CyDisplayEx界面上的图片,请问这个button里该给出什么样的方法啊? 展开
1个回答
展开全部
private void pictureBox4_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(this.Width, this.Height);
DrawToBitmap(this, bitmap, new Rectangle(0, 0, this.Width, this.Height));
bool isSave = true;
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "图片保存";
saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
isSave = false;
break;
}
}
//默认保存为JPG格式
if (imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
if (isSave)
{
try
{
images.Save(fileName, imgformat);
//MessageBox.Show("图片已经成功保存!");
}
catch
{
MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
}
}
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
/// <summary>
/// 支持呈现到指定的位图。
/// </summary>
public static Bitmap DrawToBitmap(Control control, Bitmap bitmap, Rectangle targetBounds)
{
if (bitmap == null)
{
throw new ArgumentNullException("bitmap");
}
if (((targetBounds.Width <= 0) || (targetBounds.Height <= 0)) || ((targetBounds.X < 0) || (targetBounds.Y < 0)))
{
throw new ArgumentException("targetBounds");
}
Bitmap image = new Bitmap(control.Width, control.Height, bitmap.PixelFormat);
using (Graphics graphics = Graphics.FromImage(image))
{
IntPtr hdc = graphics.GetHdc();
SendMessage(new HandleRef(control, control.Handle), 0x317, hdc, (IntPtr)30);
using (Graphics graphics2 = Graphics.FromImage(bitmap))
{
IntPtr handle = graphics2.GetHdc();
BitBlt(new HandleRef(graphics2, handle), 0, 0, control.Width, control.Height, new HandleRef(graphics, hdc), targetBounds.X, targetBounds.Y,
0xcc0020);
graphics2.ReleaseHdcInternal(handle);
}
graphics.ReleaseHdcInternal(hdc);
}
return image;
}
{
Bitmap bitmap = new Bitmap(this.Width, this.Height);
DrawToBitmap(this, bitmap, new Rectangle(0, 0, this.Width, this.Height));
bool isSave = true;
SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "图片保存";
saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveImageDialog.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "gif":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
isSave = false;
break;
}
}
//默认保存为JPG格式
if (imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
if (isSave)
{
try
{
images.Save(fileName, imgformat);
//MessageBox.Show("图片已经成功保存!");
}
catch
{
MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
}
}
}
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
/// <summary>
/// 支持呈现到指定的位图。
/// </summary>
public static Bitmap DrawToBitmap(Control control, Bitmap bitmap, Rectangle targetBounds)
{
if (bitmap == null)
{
throw new ArgumentNullException("bitmap");
}
if (((targetBounds.Width <= 0) || (targetBounds.Height <= 0)) || ((targetBounds.X < 0) || (targetBounds.Y < 0)))
{
throw new ArgumentException("targetBounds");
}
Bitmap image = new Bitmap(control.Width, control.Height, bitmap.PixelFormat);
using (Graphics graphics = Graphics.FromImage(image))
{
IntPtr hdc = graphics.GetHdc();
SendMessage(new HandleRef(control, control.Handle), 0x317, hdc, (IntPtr)30);
using (Graphics graphics2 = Graphics.FromImage(bitmap))
{
IntPtr handle = graphics2.GetHdc();
BitBlt(new HandleRef(graphics2, handle), 0, 0, control.Width, control.Height, new HandleRef(graphics, hdc), targetBounds.X, targetBounds.Y,
0xcc0020);
graphics2.ReleaseHdcInternal(handle);
}
graphics.ReleaseHdcInternal(hdc);
}
return image;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |