Save Image from user Drawn PictureBox whose SizeMode=stretch in C# winforms

六眼飞鱼酱① 提交于 2019-12-11 08:18:53

问题


Im drawing an image on picture box which is in stretch mode,I translate the mouse coordinates on the mouse click event by using a function to obtain the real coordinates and draw the image by overriding the on-paint event and using the paint event Graphics. Since the picture box is set to stretch i only obtain a small size image when i try to save the image by using picturebox.DrawtoBitmap Function.The extra parts are padded with Black colour.Please help me out.


回答1:


You can try this:

using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
                               pictureBox1.ClientSize.Height)) {
  using (Graphics g = Graphics.FromImage(bmp)) {
    g.DrawImage(yourBitmap,
                new Rectangle(0, 0, bmp.Width, bmp.Height),
                new Rectangle(0, 0, yourImage.Width, yourImage.Height),
                GraphicsUnit.Pixel);
  }
  bmp.Save(@"c:\yourfile.png", ImageFormat.Png);
}


来源:https://stackoverflow.com/questions/8698865/save-image-from-user-drawn-picturebox-whose-sizemode-stretch-in-c-sharp-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!