我有两种方法:一种处理更新图片,一种调整图片大小并绘制一个矩形。
public void updatePicture()
{
if (imageList.SelectedItem == null) return;
/*String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName); This code is moved to resizeImage*/
resizeImage(false, true);
//drawRect(); It works if I enable this code
}
但是,我试图将所有内容移到resizeImage():
private void resizeImage(Boolean draw, Boolean update)
{
if (!isValid()) return;
if (update)
{
String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName);
}
Bitmap bitmap = new Bitmap(pictureBox1.Image);
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
return;
}
int width, height;
float percentWidth = (float)pictureBox1.Width / (float)bitmap.Width;
float percentHeight = (float)pictureBox1.Height / (float)bitmap.Height;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
width = Convert.ToInt32(bitmap.Width * percent);
height = Convert.ToInt32(bitmap.Height * percent);
Bitmap cropBitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(cropBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(bitmap, 0, 0, cropBitmap.Width, cropBitmap.Height);
pictureBox1.Image = cropBitmap;
if (draw)
{
drawRect();
}
}
在这种状态下它不会工作,但为了使它工作,我只需在updatePicture()方法中调用drawRect()方法,这基本上只是移动代码。我也不是在不同时间打电话。
这是我的drawRect()方法:
private void drawRect()
{
pictureBox1.Refresh();
pictureBox1.CreateGraphics().DrawRectangle(pen, cropX, cropY, cropW, cropH);
}
任何人都能看到我的错误?
答案 0 :(得分:0)
也许这有帮助?
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
// add this line...
pictureBox1.Refresh();
return;
}
也许还低于......
pictureBox1.Image = cropBitmap;
// add this line...
pictureBox1.Refresh();
也许你看到我在做什么......在所有情况下都要调用刷新。
答案 1 :(得分:0)
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
return;
}
如果语句为真,则该代码返回,并且不会绘制。
我将resizeImage方法更改为:
private void resizeImage(Boolean draw, Boolean update)
{
if (!isValid()) return;
if (update)
{
String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName);
}
Bitmap bitmap = new Bitmap(pictureBox1.Image);
bool needsCrop = true;
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
needsCrop = false;
}
if (needsCrop)
{
int width, height;
float percentWidth = (float)pictureBox1.Width / (float)bitmap.Width;
float percentHeight = (float)pictureBox1.Height / (float)bitmap.Height;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
width = Convert.ToInt32(bitmap.Width * percent);
height = Convert.ToInt32(bitmap.Height * percent);
Bitmap cropBitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(cropBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(bitmap, 0, 0, cropBitmap.Width, cropBitmap.Height);
pictureBox1.Image = cropBitmap;
}
if (draw) { drawRect(); }
}
抱歉浪费你的时间:/