Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2016.07.24;
Скачать: CL | DM;

Вниз

GDI+ Подскажите пожалуйста, как расчитать пропорции изображения.   Найти похожие ветки 

 
M.A.   (2014-11-19 18:04) [0]

Здравствуйте. Достать из файла и отобразить в Static получилось.
Кооринаты верхнего левого угла картинки, при изменение размера картинки и Statica, определяются правильно. Только картинка пропадает, прихотитсь заново перерисовывыть после изменения размера Statica(Не в этом горе, пока что).
А вот пропорции, если ширина или высота картинки больше ширины или высоты Statica -------- ну ни как.

Подскажите пожалуйста как расчитать пропорции.

function getPicture(FormImg, HW: HWND; Tagis : TAudioGenie3): boolean;
var
 m: string;
 img: TMemoryStream;
 fg : IStream;
 imgFile{, OutImage}: TGPImage;
 Graphics : TGPGraphics;
 Brush : TGPSolidBrush;  
 tr: TRect;
 ImNachHeight , ImNachWidth : Integer;
begin
   m:=AUDIOPictureExtract(img{fg}, Tagis);
   img.Seek(0, soFromBeginning);
   fg := TMyStreamAdapter.Create(img, soReference) as IStream;
   imgFile := TGPImage.Create(fg);

   Windowdc:=GetDC(HW);
 Graphics:=TGPGraphics.Create(Windowdc);

 Brush := TGPSolidBrush.Create(MakeColor(0, 0, 0));

  GetClientRect(HW, tr);
   Graphics.FillRectangle( Brush , MakeRect(0, 0, tr.Right, tr.Bottom ));

 if tr.Right >= imgFile.GetWidth then
   ImNachWidth := (tr.Right - imgFile.GetWidth) div 2
 else
   if imgFile.GetWidth > tr.Right then
     begin
       ImNachWidth:= 0;
     end
   else
     begin
       ImNachWidth := (imgFile.GetWidth - tr.Right) div 2;
     end;

 if tr.Bottom >= imgFile.GetHeight then
   begin
     ImNachHeight := (tr.Bottom - imgFile.GetHeight) div 2;
   end
 else
   if imgFile.GetHeight > tr.Bottom then
     begin
       ImNachHeight := 0;
     end
   else
     begin
       ImNachHeight := (imgFile.GetHeight - tr.Bottom) div 2;
     end;

  Graphics.DrawImage(imgFile, ImNachWidth, ImNachHeight, imgFile.GetWidth, imgFile.GetHeight);

  imgFile.Free;//уничтожаем класс изображения
   Graphics.Free;
   ReleaseDC(FormImg,windowdc);
   img.Clear;
   Brush.Free;
   fg := nil;
end:


 
M.A.   (2014-11-19 20:14) [1]

Нашел. решение

float srcwidth = source.Width;
       float srcheight = source.Height;
       float dstwidth = width;
       float dstheight = height;

       if (srcwidth <= dstwidth && srcheight <= dstheight)  // Исходное изображение меньше целевого
       {
           int left = (width - source.Width) / 2;
           int top = (height - source.Height) / 2;
           gr.DrawImage(source, left, top, source.Width, source.Height);
       }
       else if (srcwidth / srcheight > dstwidth / dstheight)  // Пропорции исходного изображения более широкие
       {
           float cy = srcheight / srcwidth * dstwidth;
           float top = ((float) dstheight - cy) / 2.0f;
           if (top < 1.0f) top = 0;
           gr.DrawImage(source, 0, top, dstwidth, cy);
       }
       else  // Пропорции исходного изображения более узкие
       {
           float cx = srcwidth / srcheight * dstheight;
           float left = ((float) dstwidth - cx) / 2.0f;
           if (left < 1.0f) left = 0;
           gr.DrawImage(source, left, 0, cx, dstheight);
       }

       return dest;


На Delphi:

GetClientRect(HW, tr);
if ((imgFile.GetWidth <= tr.Right) and (imgFile.GetHeight <= tr.Bottom)) then // Исходное изображение меньше целевого
   begin
     ImNachWidth := (tr.Right - imgFile.GetWidth) div 2;
     ImNachHeight := (tr.Bottom - imgFile.GetHeight) div 2;
     Graphics.DrawImage(imgFile, ImNachWidth, ImNachHeight, imgFile.GetWidth, imgFile.GetHeight);
   end
 else
   if (imgFile.GetWidth / imgFile.GetHeight > tr.Right / tr.Bottom) then // Пропорции исходного изображения более широкие
     begin
       ImKonHeight := Round(imgFile.GetHeight / imgFile.GetWidth * tr.Right);
       ImNachHeight := Round((tr.Bottom - ImKonHeight) / 2.0);
       if (ImNachHeight < 1) then
         ImNachHeight := 0;
       Graphics.DrawImage(imgFile, 0, ImNachHeight, tr.Right, ImKonHeight);
     end
   else  // Пропорции исходного изображения более узкие
     begin
       ImKonWidth := Round(imgFile.GetWidth / imgFile.GetHeight * tr.Bottom);
       ImNachWidth := Round((tr.Right - ImKonWidth) / 2);
       if (ImNachWidth < 1) then
         ImNachWidth := 0;
       Graphics.DrawImage(imgFile, ImNachWidth, 0, ImKonWidth, tr.Bottom);
     end;


 
M.A.   (2014-11-19 21:13) [2]

А как сдеать, чтоб ы нарисованное не проподало при изменение размера Statica?


 
KilkennyCat ©   (2014-11-19 23:18) [3]

Static - это чегось?


 
Endy   (2014-11-20 10:35) [4]

Здесь не видно что вы делаете при WM_PAINT и откуда вызываете getPicture().
В любом случае нужно вовремя вызывать Repaint, иначе бы не затиралось.


 
M.A.   (2014-11-22 02:43) [5]


> Endy   (20.11.14 10:35) [4]
> Здесь не видно что вы делаете при WM_PAINT и откуда вызываете
> getPicture().

Вызывается по клику по ListView.
Вот и думаю как организовать рсование при изменение размера Statica.


 
M.A.   (2014-11-22 21:27) [6]

Получилось. Только вот при изменение размеров, мерцают Editы. Как исправить?



Страницы: 1 вся ветка

Текущий архив: 2016.07.24;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.014 c
15-1442439002
Юрий
2015-09-17 00:30
2016.07.24
С днем рождения ! 17 сентября 2015 четверг


4-1278405370
tytus
2010-07-06 12:36
2016.07.24
CryptoAPI (Delphi 2010, XP SP3)


11-1260089096
RusSun
2009-12-06 11:44
2016.07.24
[Error] scaledemo1.pas(124)


15-1446240602
Юрий
2015-10-31 00:30
2016.07.24
С днем рождения ! 31 октября 2015 суббота


15-1444580103
DayGaykin
2015-10-11 19:15
2016.07.24
Удивительный QuickSort