从 Picturebox 保存图像
Saving Image From Picturebox
我正在将图像绘制到图片框。我根据宽度和高度调整图片框中的图像大小,以使其正确适合 picutrebox。之后我想保存它,在保存它的同时我也想在保存的文件中保存非图像绘制部分。请看截图,在截图中我有 2 个白色部分标记为 'X'。当我将图像保存在图片框中时,我也想将空白部分(用红色标记)保存为透明 .png 或纯白色 .jpg。
实际上我从google复制了部分代码并进行了修改。如果有人请逐行解释,那将非常有用。
这是我目前所知道的,
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
Image original = Bitmap.FromFile(open.FileName);
pictureBox1.Image = new Bitmap(ScaleImage(original));
pictureBox1.Padding = new Padding((pictureBox1.Width - ScaleImage(original).Width) / 2, (pictureBox1.Height - ScaleImage(original).Height) / 2, 0, 0);
}
}
private Bitmap ScaleImage(Image oldImage)
{
double resizeFactor = 1;
if (oldImage.Width > 300 || oldImage.Height > 300)
{
double widthFactor = Convert.ToDouble(oldImage.Width) / 300;
double heightFactor = Convert.ToDouble(oldImage.Height) / 125;
resizeFactor = Math.Max(widthFactor, heightFactor);
}
int width = Convert.ToInt32(oldImage.Width / resizeFactor);
int height = Convert.ToInt32(oldImage.Height / resizeFactor);
Bitmap newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
newImage.MakeTransparent(Color.White);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.White;
pictureBox1.Image.Save("D:\temp.png", ImageFormat.Png);
}
希望对您有所帮助。尝试使用它来保存调整大小的图像(我猜是 button2_Click
)。
using (Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Transparent);
graphics.DrawImage(pictureBox1.Image, (bitmap.Width - pictureBox1.Image.Width) / 2, (bitmap.Height - pictureBox1.Image.Height) / 2);
}
bitmap.Save(@"D:\tmpMod.png", ImageFormat.Png);
}
我正在将图像绘制到图片框。我根据宽度和高度调整图片框中的图像大小,以使其正确适合 picutrebox。之后我想保存它,在保存它的同时我也想在保存的文件中保存非图像绘制部分。请看截图,在截图中我有 2 个白色部分标记为 'X'。当我将图像保存在图片框中时,我也想将空白部分(用红色标记)保存为透明 .png 或纯白色 .jpg。
实际上我从google复制了部分代码并进行了修改。如果有人请逐行解释,那将非常有用。
这是我目前所知道的,
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
Image original = Bitmap.FromFile(open.FileName);
pictureBox1.Image = new Bitmap(ScaleImage(original));
pictureBox1.Padding = new Padding((pictureBox1.Width - ScaleImage(original).Width) / 2, (pictureBox1.Height - ScaleImage(original).Height) / 2, 0, 0);
}
}
private Bitmap ScaleImage(Image oldImage)
{
double resizeFactor = 1;
if (oldImage.Width > 300 || oldImage.Height > 300)
{
double widthFactor = Convert.ToDouble(oldImage.Width) / 300;
double heightFactor = Convert.ToDouble(oldImage.Height) / 125;
resizeFactor = Math.Max(widthFactor, heightFactor);
}
int width = Convert.ToInt32(oldImage.Width / resizeFactor);
int height = Convert.ToInt32(oldImage.Height / resizeFactor);
Bitmap newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
newImage.MakeTransparent(Color.White);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.White;
pictureBox1.Image.Save("D:\temp.png", ImageFormat.Png);
}
希望对您有所帮助。尝试使用它来保存调整大小的图像(我猜是 button2_Click
)。
using (Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.Transparent);
graphics.DrawImage(pictureBox1.Image, (bitmap.Width - pictureBox1.Image.Width) / 2, (bitmap.Height - pictureBox1.Image.Height) / 2);
}
bitmap.Save(@"D:\tmpMod.png", ImageFormat.Png);
}