绘图操作时报错“无法从带有索引像素格式的图像创建 Graphics 对象”
2017-11-20 13:48
531 查看
如下代码会出现“无法从带有索引像素格式的图像创建 Graphics 对象”异常
Bitmap bitmap = new Bitmap(fileName);
Graphics graphics = Graphics.FromImage(bitmap);
解决方法为DrawImage到一个空的图片。
OpenFileDialog dialog = new OpenFileDialog
{
Filter = "Image|*.bmp;*.png;*.jpg;*.jpeg",
FilterIndex = 1
};
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileName = dialog.FileName;
Bitmap bitmap = new Bitmap(dialog.FileName);
Bitmap image = new Bitmap(bitmap.Width, bitmap.Height);// 将原图片DrawImage到新图片image,然后在image上进行绘图操作
using (Graphics graphics = Graphics.FromImage(image))
{
Rectangle destRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Rectangle srcRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
graphics.Dispose();
}
Graphics graphicsNew = Graphics.FromImage(image);
Pen pen = new Pen(Color.Red);
graphicsNew.DrawRectangle(pen, 0, 0, image.Width - 3,image.Height - 3);// 在图片上绘制矩形框
this.pictureBox1.Image = image;
}
Bitmap bitmap = new Bitmap(fileName);
Graphics graphics = Graphics.FromImage(bitmap);
解决方法为DrawImage到一个空的图片。
OpenFileDialog dialog = new OpenFileDialog
{
Filter = "Image|*.bmp;*.png;*.jpg;*.jpeg",
FilterIndex = 1
};
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileName = dialog.FileName;
Bitmap bitmap = new Bitmap(dialog.FileName);
Bitmap image = new Bitmap(bitmap.Width, bitmap.Height);// 将原图片DrawImage到新图片image,然后在image上进行绘图操作
using (Graphics graphics = Graphics.FromImage(image))
{
Rectangle destRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Rectangle srcRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
graphics.Dispose();
}
Graphics graphicsNew = Graphics.FromImage(image);
Pen pen = new Pen(Color.Red);
graphicsNew.DrawRectangle(pen, 0, 0, image.Width - 3,image.Height - 3);// 在图片上绘制矩形框
this.pictureBox1.Image = image;
}
相关文章推荐
- (转载)无法从带有索引像素格式的图像创建graphics对象|A Graphics object cannot be created from an image that has an indexed pixel format
- 无法从带有索引像素格式的图像创建graphics对象
- .net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
- 无法从带有索引像素格式的图像创建graphics对象
- .net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
- 无法从带有索引像素格式的图像创建graphics对象|A Graphics object cannot be created from an image that has an indexed pixel format
- Bitmap添加水印提示【无法从带有索引像素格式的图像创建Graphics对象】
- 无法从带有索引像素格式的图像创建graphics对象
- 生成水印图片是出现这个问题!(无法从带有索引像素格式的图像创建 Graphics 对象。)
- "无法从带有索引像素格式的图像创建graphics对象" 解决
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象(转)
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象
- .net下灰度模式图像在创建Graphics时出现:无法从带有索引像素格式的图像创建graphics对象 问题的解决方案。
- 给图片添加水印,解决GIF添加水印的问题(无法从带有索引像素格式的图像创建 Graphics 对象)
- 无法从带有索引像素格式的图像创建 Graphics 对象。
- 无法从带有索引像素格式的图像创建graphics对象
- 无法从带有索引像素格式的图像创建graphics对象
- "无法从带有索引像素格式的图像创建graphics对象"