Форум программистов «Весельчак У»
  *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

  • Рекомендуем проверить настройки временной зоны в вашем профиле (страница "Внешний вид форума", пункт "Часовой пояс:").
  • У нас больше нет рассылок. Если вам приходят письма от наших бывших рассылок mail.ru и subscribe.ru, то знайте, что это не мы рассылаем.
   Начало  
Наши сайты
Помощь Поиск Календарь Почта Войти Регистрация  
 
Страниц: [1]   Вниз
  Печать  
Автор Тема: (faq)Как отобразить картинку из файла?  (Прочитано 11940 раз)
0 Пользователей и 1 Гость смотрят эту тему.
PSD
Главный специалист

ru
Offline Offline
Пол: Мужской

« : 24-02-2005 11:47 » 

Для меня это всегда было камнем предткновения выводить изобраение из файла на экран в винде .....

И вот видимо настал день когда с этим пробелом нужно попончить.

Нужно организовать просмоторщик картинок на подобе ACDS, конечно не стаки богатым функционалом но с немение широким диапозоном поддерживаемых форматов. Самому ручкам разбирать форматы очень не хочется.

Чем? каким компонентом или библиотекой функции  можно воспользоваться для замещение цепочки у которой в начале путь к файлу а в конце  картинка на экране или хотябы DC (BITMAP) которые остается только отмапить в нужное место?

(ейбогу если ничего хорошего не посоветуете буду влеплять в форму эекплорер и генерировать для него страничку с блоком <IMG...)
   
 
« Последнее редактирование: 01-05-2007 09:13 от Алексей1153++ » Записан

Да да нет нет все остальное от лукавого.
Джон
просто
Администратор

de
Offline Offline
Пол: Мужской

« Ответ #1 : 24-02-2005 12:20 » 

PSD, спокойно, ща насоветуем Ага

Если есть возможность - используй gdi+ - поддерживает многие форматы:


void CImagePreviewWnd::OnPaint()
{
   CPaintDC dc(this);

   CDC *pDC = &dc;

   // Fill the preview area with a WHITE background.
   RECT rect;
   GetClientRect(&rect);
   rect.left += 1;
   rect.top += 1;
   rect.right -= 1;
   rect.bottom -= 1;
   pDC->FillSolidRect(&rect, RGB(255,255,255));

   if(m_stImgFile.IsEmpty()) return;

   USES_CONVERSION;

   UINT uiWidth, uiHeight;
   int iAdjTop , iAdjLeft;

   double dRatio;
   
   iAdjTop = 1;
   iAdjLeft = 1;

   // Initialize the Graphics class in GDI+.
   Graphics graphics(pDC->m_hDC);

   // Use the Image class to display a thumbnail of the image.
   Image image(T2CW(m_stImgFile));
   if(image.GetLastStatus()!=Ok) return;

   // Determine the appropriate size of the thumbnail preview
   // given the image size ratio and the preview window size ratio.
   uiWidth = image.GetWidth();
   uiHeight = image.GetHeight();
   dRatio = ((double)uiWidth)/((double)uiHeight);

   // If the width is larger than the height of the image,
   // set the width of the thumbnail to the width of the preview area
   // and calculate the thumbnail height by using the ratios.
   // If the height is larger than the width of the image,
   // set the height of the thumbnail to the height of the preview area
   // and calculate the thumbnail width by using the ratios.

   if(m_bThumb)
   {
      UINT thumbWidth, thumbHeight;

      if (uiWidth > uiHeight)
      {
         thumbWidth = rect.right - rect.left;
         thumbHeight = (UINT)(thumbWidth/dRatio);

         if (thumbHeight == 0) thumbHeight = 1;
         if (thumbHeight > (UINT)(rect.bottom - rect.top)) thumbHeight = rect.bottom - rect.top;

         // Adjust things to make the preview not paint over the control border.
         iAdjTop = 1 + ((rect.bottom - rect.top)/2) - (thumbHeight/2);
         iAdjLeft = 1;
      }
      else
      {
         thumbHeight = rect.bottom - rect.top;
         thumbWidth = (UINT)(uiWidth*thumbHeight/uiHeight);

         if (thumbWidth == 0) thumbWidth = 1;
         if (thumbWidth > (UINT)(rect.right - rect.left)) thumbWidth = rect.right - rect.left;

         // Adjust things to make the preview not paint over the control border.
         iAdjTop = 1;
         iAdjLeft = 1 + ((rect.right - rect.left)/2) - (thumbWidth/2);
      }

      // Get the thumbnail and display it in the preview control.
      Image* pThumbnail = image.GetThumbnailImage(thumbWidth, thumbHeight, NULL, NULL);
      graphics.DrawImage(pThumbnail, iAdjLeft, iAdjTop, pThumbnail->GetWidth(), pThumbnail->GetHeight());
      delete pThumbnail;
   }
   else
   {
      graphics.DrawImage(&image, iAdjLeft, iAdjTop, uiWidth, uiHeight);
   }
}
Записан

Я вам что? Дурак? По выходным и праздникам на работе работать. По выходным и праздникам я работаю дома.
"Just because the language allows you to do something does not mean that it’s the correct thing to do." Trey Nash
"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard P. Feynman
"All science is either physics or stamp collecting." Ernest Rutherford
"Wer will, findet Wege, wer nicht will, findet Gründe."
Джон
просто
Администратор

de
Offline Offline
Пол: Мужской

« Ответ #2 : 24-02-2005 12:29 » 

2. Если c gdi, и предполагается наличие ИЕ в системе, то можно воспользоваться объектом
IPicture - маленько покорявее, но тоже работает - (забыл - вроде как с аним. гифами не работает - в проекте задача не стояла их показывать)

B .h
   LPPICTURE   m_pPicture;

   SIZE      m_sizeInPix;
   SIZE      m_sizeInHiMetric;

   CRect      m_rc;

   int         m_nWidthInHiMetric;   // rectangle Width  in HI METRIC
   int         m_nHeightInHiMetric;// rectangle Height in HI METRIC

   CString      m_stImgName;
.cpp


CImgItem::CImgItem()
{
   m_pPicture = NULL;
   m_nWidthInHiMetric = 10;
   m_nHeightInHiMetric = 10;
}

BOOL CImgItem::InitImg(CString stImgName, CString stImgFile, CRect rc, BOOL bAutoSize)
{
   IPicture *pPic = NULL;

   USES_CONVERSION;
   HRESULT hr = ::OleLoadPicturePath(const_cast<LPOLESTR>(T2COLE(stImgFile)),
                             NULL,
                             0,
                             0,
                             IID_IPicture,
                             reinterpret_cast<LPVOID *>(&pPic));
   
   if (SUCCEEDED(hr) && pPic != NULL)
   {
      ASSERT(m_pPicture == NULL);

      m_pPicture = pPic;

      // get width and height of picture
      VERIFY(SUCCEEDED(m_pPicture->get_Width(&m_sizeInHiMetric.cx)));
      VERIFY(SUCCEEDED(m_pPicture->get_Height(&m_sizeInHiMetric.cy)));

      const int HIMETRIC_PER_INCH = 2540;

      const HDC hDCScreen = ::GetDC(NULL);
      ASSERT(hDCScreen != NULL);
      // Pixels per logical inch along width
      const int nPixelsPerInchX = ::GetDeviceCaps(hDCScreen, LOGPIXELSX);
      // Pixels per logical inch along height
      const int nPixelsPerInchY = ::GetDeviceCaps(hDCScreen, LOGPIXELSY);
      VERIFY(::ReleaseDC(NULL, hDCScreen) != 0);

      // convert himetric to pixels
      m_sizeInPix.cx = (nPixelsPerInchX * m_sizeInHiMetric.cx +
                    HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
      m_sizeInPix.cy = (nPixelsPerInchY * m_sizeInHiMetric.cy +
                    HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;

      m_stImgName = stImgName;
      m_rc = rc;

      if(bAutoSize)
      {
         m_rc.right = m_rc.left+m_sizeInPix.cx;
         m_rc.bottom = m_rc.top+m_sizeInPix.cy;
      }

      // Convert m_rc Width and Height to Metric
      // convert pixels to metric
      m_nWidthInHiMetric = (m_rc.Width()*HIMETRIC_PER_INCH - HIMETRIC_PER_INCH/2)/nPixelsPerInchX;
      m_nHeightInHiMetric = (m_rc.Height()*HIMETRIC_PER_INCH - HIMETRIC_PER_INCH/2)/nPixelsPerInchY;

   }
   else
   {
      // OleLoadPicturePath failed.
      ::AfxMessageBox("Error", MB_OK | MB_ICONHAND);
      return FALSE;
   }

   return TRUE;
}

void CImgItem::DrawImg(CDC *pDC)
{
   if (m_pPicture != NULL)
   {
      // get palette
      HPALETTE hPal = NULL;
      VERIFY(SUCCEEDED(m_pPicture->get_hPal(reinterpret_cast<OLE_HANDLE *>(&hPal))));

      CPalette *pPalOld = NULL;
      if (hPal != NULL)
      {
         pPalOld = pDC->SelectPalette(CPalette::FromHandle(hPal), FALSE);
         pDC->RealizePalette();
      }

      // transparent?
      DWORD dwAttr = 0;
      if (FAILED(m_pPicture->get_Attributes(&dwAttr)) ||
         (dwAttr & PICTURE_TRANSPARENT))
      {
         // use an off-screen DC to prevent flickering
         CDC MemDC;
         VERIFY(MemDC.CreateCompatibleDC(pDC));
         CBitmap Bmp;
         VERIFY(Bmp.CreateCompatibleBitmap(pDC,
                                   m_sizeInPix.cx,
                                   m_sizeInPix.cy));

         CBitmap *pBmpOld = MemDC.SelectObject(&Bmp);
         CPalette *pPalMemOld = NULL;
         if (hPal != NULL)
         {
            pPalMemOld = MemDC.SelectPalette(CPalette::FromHandle(hPal), FALSE);
            MemDC.RealizePalette();
         }

         const RECT rc = { 0, 0, m_sizeInPix.cx, m_sizeInPix.cy };

         MemDC.FillSolidRect(&rc, ::GetSysColor(COLOR_WINDOW));

         // display picture using IPicture::Render
         VERIFY(SUCCEEDED(m_pPicture->Render(MemDC.GetSafeHdc(),
                                         0,
                                         0,
                                         m_sizeInPix.cx,
                                         m_sizeInPix.cy,
                                         0,
                                         m_sizeInHiMetric.cy,
                                         m_sizeInHiMetric.cx,
                                         -m_sizeInHiMetric.cy,
                                         &rc)));

         VERIFY(pDC->BitBlt(0, 0, m_sizeInPix.cx, m_sizeInPix.cy,
                        &MemDC, 0, 0, SRCCOPY));

         MemDC.SelectObject(pBmpOld);

         if (pPalMemOld != NULL)
         {
            ASSERT(hPal != NULL);
            MemDC.SelectPalette(pPalMemOld, FALSE);
         }
      }
      else
      {
         // display picture using IPicture::Render
         VERIFY(SUCCEEDED(m_pPicture->Render(pDC->GetSafeHdc(),
                                         m_rc.left,//0,
                                         m_rc.top,//0,
                                         m_rc.Width(),//m_sizeInPix.cx,
                                         m_rc.Height(),//m_sizeInPix.cy,
                                         0,
                                         m_sizeInHiMetric.cy,
                                         m_nWidthInHiMetric,//m_sizeInHiMetric.cx,
                                         -m_nHeightInHiMetric,//-m_sizeInHiMetric.cy,
                                         &m_rc)));
      }

      if (pPalOld != NULL)
      {
         ASSERT(hPal != NULL);
         pDC->SelectPalette(pPalOld, FALSE);
      }
   }
}

void CImgItem::ClearImg()
{
   if (m_pPicture != NULL)
   {
      m_pPicture->Release();
      m_pPicture = NULL;
   }
}

зы если что-то непонятно - спрашивай, всё расписывать времени нету, сорри
ну и конечно посмотри codeguru codeproject там такого добра навалом
« Последнее редактирование: 24-02-2005 12:38 от Джон » Записан

Я вам что? Дурак? По выходным и праздникам на работе работать. По выходным и праздникам я работаю дома.
"Just because the language allows you to do something does not mean that it’s the correct thing to do." Trey Nash
"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard P. Feynman
"All science is either physics or stamp collecting." Ernest Rutherford
"Wer will, findet Wege, wer nicht will, findet Gründe."
PSD
Главный специалист

ru
Offline Offline
Пол: Мужской

« Ответ #3 : 24-02-2005 12:56 » 

Какие форматы поддерживает такое решение ?
Записан

Да да нет нет все остальное от лукавого.
Джон
просто
Администратор

de
Offline Offline
Пол: Мужской

« Ответ #4 : 24-02-2005 13:16 » 

gdi+ MSDN

You can create Image objects based on files of a variety of types including BMP, GIF, JPEG, PNG, TIFF, and EMF.

IPicture - использовал для BMP и JPEG, это из старого проекта ( ~3 года ) поэтому точно не помню, но кажется он зависит от системы - чем больше тем лучше - и может, то что и ИЕ (в башке почему-то всплывает, что проблемы с аним гифами, но какие не знаю)

« Последнее редактирование: 24-02-2005 13:24 от Джон » Записан

Я вам что? Дурак? По выходным и праздникам на работе работать. По выходным и праздникам я работаю дома.
"Just because the language allows you to do something does not mean that it’s the correct thing to do." Trey Nash
"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard P. Feynman
"All science is either physics or stamp collecting." Ernest Rutherford
"Wer will, findet Wege, wer nicht will, findet Gründe."
Джон
просто
Администратор

de
Offline Offline
Пол: Мужской

« Ответ #5 : 24-02-2005 13:47 » new

Первый проще да и автоматическая генерация миниатюры тоже встроена.

По второму посмотри:
http://www.codeguru.com/Cpp/G-M/bitmap/otherformats/print.php/c4921/
http://www.codeproject.com/bitmap/imgviewer.asp
http://www.codeproject.com/bitmap/cpicture.asp
http://www.codeproject.com/bitmap/pictureshow.asp

Гигантская ценность этих статей - их обсуждение. Забыл ещё - IPicture иконки тоже может.
Записан

Я вам что? Дурак? По выходным и праздникам на работе работать. По выходным и праздникам я работаю дома.
"Just because the language allows you to do something does not mean that it’s the correct thing to do." Trey Nash
"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard P. Feynman
"All science is either physics or stamp collecting." Ernest Rutherford
"Wer will, findet Wege, wer nicht will, findet Gründe."
Страниц: [1]   Вверх
  Печать  
 

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines