aspect ratio of the image is preserved when the image is scaled.

(Thanks to px268 for the code)
This commit is contained in:
kenz0 2009-08-28 07:42:28 +00:00
parent c6fdbf7b60
commit 5a4304f020
2 changed files with 70 additions and 9 deletions

View File

@ -37,7 +37,9 @@ using namespace Gdiplus;
CMeterImage::CMeterImage(CMeterWindow* meterWindow) : CMeter(meterWindow)
{
m_Bitmap = NULL;
m_DimensionsDefined = false;
m_WidthDefined = false;
m_HeightDefined = false;
m_PreserveAspectRatio = false;
m_hBuffer = NULL;
m_Modified.dwHighDateTime = 0;
m_Modified.dwLowDateTime = 0;
@ -155,10 +157,28 @@ void CMeterImage::LoadImage(bool bLoadAlways)
if (m_Bitmap)
{
if (!m_DimensionsDefined)
// Calculate size of the meter
int imageW = m_Bitmap->GetWidth();
int imageH = m_Bitmap->GetHeight();
if (m_WidthDefined)
{
m_W = m_Bitmap->GetWidth();
m_H = m_Bitmap->GetHeight();
if (!m_HeightDefined)
{
m_H = (imageW == 0) ? 0 : m_W * imageH / imageW;
}
}
else
{
if (m_HeightDefined)
{
m_W = (imageH == 0) ? 0 : m_H * imageW / imageH;
}
else
{
m_W = imageW;
m_H = imageH;
}
}
}
}
@ -189,9 +209,15 @@ void CMeterImage::ReadConfig(const WCHAR* section)
}
m_ImageName = m_MeterWindow->MakePathAbsolute(m_Path + m_ImageName);
if (-1 != parser.ReadInt(section, L"W", -1) && -1 != parser.ReadInt(section, L"H", -1))
m_PreserveAspectRatio = 0!=parser.ReadInt(section, L"PreserveAspectRatio", 0);
if (-1 != parser.ReadInt(section, L"W", -1))
{
m_DimensionsDefined = true;
m_WidthDefined = true;
}
if (-1 != parser.ReadInt(section, L"H", -1))
{
m_HeightDefined = true;
}
}
@ -240,8 +266,41 @@ bool CMeterImage::Draw(Graphics& graphics)
// Copy the image over the doublebuffer
int x = GetX();
int y = GetY();
Rect r(x, y, m_W, m_H);
graphics.DrawImage(m_Bitmap, r, 0, 0, m_Bitmap->GetWidth(), m_Bitmap->GetHeight(), UnitPixel);
int imageW = m_Bitmap->GetWidth();
int imageH = m_Bitmap->GetHeight();
int drawW, drawH;
if (m_PreserveAspectRatio)
{
if (imageW == 0 || imageH == 0 || m_W == 0 || m_H == 0) return true;
REAL imageRatio = imageW / (REAL)imageH;
REAL meterRatio = m_W / (REAL)m_H;
if (imageRatio >= meterRatio)
{
drawW = m_W;
drawH = m_W * imageH / imageW;
}
else
{
drawW = m_H * imageW / imageH;
drawH = m_H;
}
// Centering
x += (m_W - drawW) / 2;
y += (m_H - drawH) / 2;
}
else
{
drawW = m_W;
drawH = m_H;
}
Rect r(x, y, drawW, drawH);
graphics.DrawImage(m_Bitmap, r, 0, 0, imageW, imageH, UnitPixel);
}
return true;

View File

@ -45,7 +45,9 @@ private:
Gdiplus::Bitmap* m_Bitmap; // The bitmap
std::wstring m_ImageName; // Name of the image
std::wstring m_Path;
bool m_DimensionsDefined;
bool m_WidthDefined;
bool m_HeightDefined;
bool m_PreserveAspectRatio; // If true, aspect ratio of the image is preserved when the image is scaled
HGLOBAL m_hBuffer;
FILETIME m_Modified;
};