简体   繁体   中英

How can I draw a small image over an image using Graphics library C# ?

When I try to draw an image over another one using Graphics library C# it scale the small one and cover the first one:

  public Form1()
    {   
        //InitializeComponent();
        read_file();
        InitializeComponent1();
        SetStyle(ControlStyles.Opaque, true);
        // theImage = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/tryImage.jpg");
        theImage2 = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/1.jpg");
        // theImage = new Bitmap(newImage);
        theImage = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/tryImage.jpg");
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        //e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        //e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 
          g.DrawImage(theImage, ClientRectangle);
        // Create pen.
        g.FillRectangle(new SolidBrush(Color.Red), 50, 50, 50, 50);
        RectangleF recto = new System.Drawing.RectangleF(50, 50, 50, 50);
        Pen blackPen = new Pen(Color.Black,1);

        g.DrawRectangle(blackPen, 50, 50, 50, 50);
        g.DrawImage(theImage2, ClientRectangle); //this will cover the 1st one
    }

Have this instead:

g.DrawImage(theImage2, 0, 0, theImage2.Width, theImage2.Height);

This should draw the image in the "proper" place without stretching it.

look at draw on image

insteed DrawString you should use DrawImage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM