将图像居中放置在另一个图像C#上 [英] Center image on another Image C#
问题描述
我对C#GDI +图形还很陌生,
Hi I am fairly new to C# GDI+ graphics,
我想在另一幅图像上绘制图像,该图像应在图像上固定高度和宽度的容器中水平和垂直居中
I want to draw an image over another, which should be centered horizontally and vertically in a fixed height and width container on an image,
我试图通过水平居中来做到这一点,并且输出结果很奇怪.
I tried to do this with horizontal centering and the output is weird..
我正在分享我的尝试方式的注释代码,让我知道是否有更简单的方法,我只想缩放图像并居中..
I am sharing the commented code of how I am trying to do it, let me know if there is any easier way to do it, I just want to scale and center the image ..
//The parent image resolution is 4143x2330
//the container for child image is 2957x1456
Image childImage = Image.FromFile(path.Text.Trim());
Image ParentImage = (Image)EC_Automation.Properties.Resources.t1;
Bitmap bmp2 = (Bitmap)ParentImage;
Graphics graphic = Graphics.FromImage(ParentImage);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
double posX = (2957 / 2.0d) - (childImage.Width / 2.0d);
//HAlf of the container size - Half of the image size
//should make it center in container
graphic.DrawImage((Image)childImage,
new Rectangle(new Point((int)posX, 420), new Size( 2957, 1456))); //Drawing image
推荐答案
解决了这个问题,我正在绘制固定宽度的图像,而基于宽高比和新高度,它应该具有新的宽度
Solved it , I was drawing the image of fixed width, whereas it should have been with a new width based on the aspect ratio and new height,
我也在尝试从Container中查找图像的中心,它应该是整个父图像的中心
Also I was taking trying to find the center of the image from Container, which should have been the center of the whole parent Image
这篇关于将图像居中放置在另一个图像C#上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!