在另一张图像上居中图像 [英] Center image on another image

查看:49
本文介绍了在另一张图像上居中图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 C# GDI+ 图形还很陌生.

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,

我还试图从容器中找到图像的中心,它应该是整个父图像的中心

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

这篇关于在另一张图像上居中图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆