开发者

C#, Bitmap becomes twice bigger, why?

开发者 https://www.devze.com 2023-02-15 02:17 出处:网络
I\'ve got this code: public void rotateRocketImage() { Bitmap b = this.rocketImgOriginal; //create a new empty bitmap to hold rotated image

I've got this code:

    public void rotateRocketImage()
    {
        Bitmap b = this.rocketImgOriginal;

            //create a new empty bitmap to hold rotated image
        Bitmap tempBitmap = new Bitmap(97,97);

            //make a graphics object from the empty bitmap
        Graphics g = Graphics.FromImage(tempBitmap);

            //move rotation point to cent开发者_如何学Goer of image
        //g.TranslateTransform(48, 48);

            //rotate
        //g.RotateTransform(this.orient);

            //move image back
        //g.TranslateTransform(-48, -48);

            //draw passed in image onto graphics object
        g.DrawImage(b,0,0);

        this.rocketImg = tempBitmap;
    }

which ( with RotateTransform currently disables ) should just make this.rocketImg equal to this.rocketImg , yet somehow it enlarges the picture almost twice... any ideas what could be causing it?

Thanks!

C#, Bitmap becomes twice bigger, why?

edit: here's the drawing code:

        private void timer1_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }


    protected override void OnPaint(PaintEventArgs e)
    {

        var tempRocket = new Bitmap( rocket.rocketImg );

        using (var g = Graphics.FromImage(tempRocket))
        {
            e.Graphics.DrawImage(tempRocket, 150, 150);
        }

    }


There is a resolution parameter in bitmap image.

If your bitmaps have different resolutions you receive the deformation when draw one image on other.

See HorizontalResolution and VerticalResolution properties and SetResolution method of Bitmap instance.

Code sample which shows how it work:

        int magnificationIndex = 2;

        Bitmap tempRocket = new Bitmap("ccc.bmp");

        Bitmap tempBitmap = new Bitmap(97, 97);

        tempBitmap.SetResolution(tempRocket.HorizontalResolution * magnificationIndex,
            tempRocket.VerticalResolution * magnificationIndex);


        using (Graphics g = Graphics.FromImage(tempBitmap))
        {
            g.FillRectangle(Brushes.White, 0, 0, 97, 97);
            g.DrawImage(tempRocket,0,0);
        }

        tempBitmap.Save("result.bmp");


Looks to me like the problem is with the constructor. This is a link to the class definition in MSDN: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx You should specify either a Graphics object or just set the resolution manualy.

Alternativly, you can just specify the original image in the constructor and the new object will inharit it`s properties.


As you said original size of image is 97 X 97 but you are drawing it with 150 X 150 which makes it larger.


It might be that the control that you are drawing this into has the SizeMode set to Zoom.


I think you should review the properties of the rocketImg control. Make sure it doesn't stretch, resize.

0

精彩评论

暂无评论...
验证码 换一张
取 消