Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。 g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), GraphicsUnit.Pixel);
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Controls.Add(label1);
pictureBox1.Controls.Add(label9);
label1.BackColor = Color.Transparent;//透明底色
label1.Location = new Point(885, 100);
label1.AutoSize = true;
} 点击button 进行保存
private void button1_Click(object sender, EventArgs e)
{
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
{
Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。
g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), GraphicsUnit.Pixel);
pictureBox1.Image.Save(fileName, imgformat);
MessageBox.Show("图片已经成功保存!");
}
catch
{
MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
}
}
}
}
}
}
Graphics g = Graphics.FromImage(pictureBox1.Image);
Pen pen=new Pen(Color.Crimson);
Brush brush=new SolidBrush(Color.Cyan);
Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
g.DrawString("求分求鼓励", drawFont, brush, 0, 0);
pictureBox1.Image.Save(@"C:\Users\ll\Desktop\1.jpg");
private void btnSave_Click(object sender, EventArgs e)
{
Image img = (Image)pictureBox1.Image.Clone();
using (Brush brush = new SolidBrush(label1.ForeColor))
using (Graphics g = Graphics.FromImage(img))
{
Rectangle rect = new Rectangle(label1.Left - pictureBox1.Left, label1.Top- pictureBox1.Top, label1.Width, label1.Height);
if (label1.BackColor != Color.Transparent)
{
using (Brush bgBrush = new SolidBrush(label1.BackColor))
{
g.FillRectangle(bgBrush, rect);
}
}
g.DrawString(label1.Text, label1.Font, brush, rect, StringFormat.GenericDefault);
g.Save();
}
img.Save("d:\\abc.png", System.Drawing.Imaging.ImageFormat.Png);
}