2

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
    }
Dmitry
  • 279
  • 2
  • 10
Emykindman
  • 51
  • 2
  • 6

2 Answers2

5

Have this instead:

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

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

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • :D it's working :D ,thanks very much ,i have another issue, would you tell me if i want to repeat this work several times ,depend on update of a file when the scene changes, so do i need to call this function OnPaint many times?? , actually i don't know the mechanism which this function use .. – Emykindman Dec 29 '10 at 16:24
  • @Emykindman having this in the OnPaint is very "heavy" you should really avoid it - assumed you had good reason to have it in there. You can use PictureBox control instead and change its bitmap whenever the file changes. – Shadow The GPT Wizard Dec 30 '10 at 06:12
  • ok , i'm using pictureboxes now but it never view the scene until finish all the work , i mean if i want to draw each change in the scene it view all the changes at the end . so please how can i solve this :) – Emykindman Dec 30 '10 at 13:26
  • @Emykindman not sure what you mean.. can you post some code? (edit your original question with the new code) – Shadow The GPT Wizard Dec 30 '10 at 13:28
  • here is my question http://stackoverflow.com/questions/4563014/how-to-re-draw-the-bicturebox-in-the-run-time – Emykindman Dec 30 '10 at 13:40
0

look at draw on image

insteed DrawString you should use DrawImage

Sergey K
  • 4,071
  • 2
  • 23
  • 34
  • 2
    Where do you see `DrawString`?? – Shadow The GPT Wizard Dec 29 '10 at 12:38
  • Create an object of System.Drawing.Graphics and this object has method DrawString look at http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring(VS.71).aspx – Sergey K Jan 13 '11 at 07:55
  • 1
    @Shadow Wizard was asking about the code posted in the original question. Where does `DrawString` appear in that code? You said (s)he should use `DrawImage` instead of `DrawString`, but (s)he isn't using `DrawString` in the first place. – Cody Gray - on strike Jan 13 '11 at 09:56