pictureBox显示图像 自动调整大小 缩放自适应 画图位置自适应
/// <summary>
/// 加载的图像变量
/// </summary>
Image img;
public Form1()
{
//把图像的缩放交给 pictureBox 自行调整
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
}
double pfRatio = 0.0;//重要的参数;作图 自适应就靠它了
private void button1_Click(object sender, EventArgs e)
{
img = Image.FromFile("1.jpg");
pictureBox1.BackgroundImage = img;
//pictureBox1 最大的显示范围
int preW = 500;
int preH = 1000;
if ((double)preW / img.Width > (double)preH / img.Height)
{
pfRatio = (double)preH / img.Height;
pictureBox1.Size = new Size((int)(img.Width * pfRatio), preH);
}
else
{
pfRatio = (double)preW / img.Width;
pictureBox1.Size = new Size(preW, (int)(img.Height * pfRatio));
}
pictureBox1.Invalidate();
}
/*如果有在pictureBox1上作图,那也跟随调整位置和参数*/
//画str的数量
int StrShowCount = 0;
//画str的行间距
int StrShowInterval = 13;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int r = 2;
int penW = 2;
StrShowCount = 0;
Graphics g = e.Graphics;
g.DrawString("HF,GG GL", Font, Brushes.Beige, new Point(0, (StrShowCount++) * StrShowInterval));
g.DrawString("1.jpg", Font, Brushes.Beige, new Point(0, (StrShowCount++) * StrShowInterval));
g.DrawEllipse(new Pen(Brushes.Beige, penW), new Rectangle(new Point((int)(200 * pfRatio) - r, (int)(200 * pfRatio) - r), new Size(r * 2, r * 2)));
}