首页 > 其他分享 >图像处理界面--缩略图的显示

图像处理界面--缩略图的显示

时间:2022-12-25 21:31:09浏览次数:56  
标签:DesRect 缩略图 -- void clRect ListThumbnail 图像处理 NULL VectorImageNames

​http://pan.baidu.com/s/1i5JGBFB​

编写一个图像处理类的程序,实现缩略图是非常自然的想发。基于mfc+opencv框架,实现以下代码

style child border none

一、填写界面 主要实现图片的添加、删除、显示

图像处理界面--缩略图的显示_Test

左边是listctrl控件(alignment为top),右边是textbox控件(client edge 为true)。textbox控件主要用来占个位置,具体显示的时候要进行重载处理。

二、实现缩略图

1)最方便使用的缩略图函数是Gdiplus下的  GetThumbnailImage 函数

        Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, NULL, NULL));

能够将大图转换为小图。为了使用Gdiplus,首先需要引入和初始化

#include <gdiplus.h>

using namespace Gdiplus;

#pragma comment(lib, "gdiplus.lib")   

ULONG_PTR GdiplusToken;

GdiplusStartupInput gdiplusStartupInput; 

在initdialog中

GdiplusStartup(&GdiplusToken, &gdiplusStartupInput, NULL);

 

2)添加图片函数,主要是将文件目录名push到 m_VectorImageNames 结构中

void CGOthumbnailDlg::OnBnClickedButtonAddimg()

{

    CString szFilters= _T("*(*.*)|*.*|jpg(*.jpg)|*.jpg|bmp(*.bmp)|*.bmp||");

    CFileDialog dlgFile(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilters,NULL,0,TRUE); 

    CString FilePathName ;

    if(dlgFile.DoModal()==IDOK){

        FilePathName = dlgFile.GetPathName();

    }

    m_VectorImageNames.push_back(FilePathName);

    DrawThumbnails();

}

 

3)为了图片保存在imagelist中,并且使用listctrl控件进行显示,并且在初始化的时候进行定义

    m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);

    m_ListThumbnail.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);

具体绘图函数

void CGOthumbnailDlg::DrawThumbnails(void)

{

    CBitmap*    pImage = NULL;

    HBITMAP        hBmp = NULL;

    POINT        pt;

    CString        strPath;

 

    // no images

    if (m_VectorImageNames.empty())

        return;

    //不自动刷新

    m_ListThumbnail.SetRedraw(FALSE);

 

    // reset our image list

    for (int i = 0; i<m_ImageListThumb.GetImageCount(); i++)

        m_ImageListThumb.Remove(i);    

 

    // remove all items from list view

    if (m_ListThumbnail.GetItemCount() != 0)

        m_ListThumbnail.DeleteAllItems();

 

    // set the size of the image list

    m_ImageListThumb.SetImageCount(m_VectorImageNames.size());

    

    // draw the thumbnails

    for (int i = 0;i<m_VectorImageNames.size();i++)

    {        

        USES_CONVERSION;

        Bitmap img( m_VectorImageNames[i].AllocSysString());

        //使用GDI直接获得thumbnail

        Bitmap* pThumbnail = static_cast<Bitmap*>(img.GetThumbnailImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, NULL, NULL));

        // attach the thumbnail bitmap handle to an CBitmap object

        pThumbnail->GetHBITMAP(NULL, &hBmp);

        pImage = new CBitmap();         

        pImage->Attach(hBmp);

        m_ImageListThumb.Replace(i, pImage, NULL);

        m_ListThumbnail.InsertItem(i, m_VectorImageNames[i], i);

        delete pImage;

        delete pThumbnail;

    }

    //显示thumbnail

    m_ListThumbnail.SetRedraw(); 

}

4)点击图片能够显示大图

void CGOthumbnailDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)

{

    // retrieve message info.

    LPNMITEMACTIVATE pItemAct = (LPNMITEMACTIVATE)pNMHDR;

 

    // determine which item receives the click

    LVHITTESTINFO  hitTest;

    ZeroMemory(&hitTest, sizeof(LVHITTESTINFO));

    hitTest.pt = pItemAct->ptAction;

    m_ListThumbnail.SendMessage(LVM_SUBITEMHITTEST, 0, (LPARAM)&hitTest);

 

    // draw the selected image

    if ( hitTest.iItem >= 0)

    {

        m_nSelectedItem = hitTest.iItem;

        DrawSelectedImage();

    }

 

    // select the item clicked

    *pResult = 0;    

}

void CGOthumbnailDlg::DrawSelectedImage(void)

{

    CString        strPath;

    Rect        DesRect;

    RECT        clRect;

 

    USES_CONVERSION;

    Image img(m_VectorImageNames[m_nSelectedItem].AllocSysString() );

 

    // get destination rectangle

    m_imagerect.GetClientRect(&clRect);

    DesRect.X = clRect.left;

    DesRect.Y = clRect.top;

    DesRect.Width = clRect.right - clRect.left;

    DesRect.Height = clRect.bottom - clRect.top;

 

    // draw the image

    Graphics gc( m_imagerect.GetDC()->GetSafeHdc() );

    gc.DrawImage(&img, DesRect);

}

5)重载textbox的基类,改为imagearea类,主要是重载paint函数

void CImageArea::OnPaint() 

{

    CPaintDC dc(this); // device context for painting

    // TODO: Add your message handler code here

    CGOthumbnailDlg*  pWnd = static_cast<CGOthumbnailDlg*>(GetParent());

    if(pWnd->m_ListThumbnail.GetItemCount() != 0)

     pWnd->DrawSelectedImage();

    // Do not call CStatic::OnPaint() for painting messages

}

三、实现结果和小结

这个程序看似只是解决一个简单的问题,但是涉及到的东西很多。能够比较好的解决关键是找到了比较好的参考代码。同时也是因为我在很久之前就发现了这个问题,并且积极地去寻找解决方法。这两点应该都是最后得到比较好的结果必不可少的。

下一步就是需要把这样的程序进行整理整合,更好方便复用。

标签:DesRect,缩略图,--,void,clRect,ListThumbnail,图像处理,NULL,VectorImageNames
From: https://blog.51cto.com/jsxyhelu2017/5968402

相关文章

  • cs跨站请求伪造 csrf添加装饰器的多种方式 auth认证模块 auth认证相关模块及操作 拓展
    目录csrf跨站请求伪造csrf相关校验策略1.form表单csrf策略2.ajax请求csrf策略方式一:方式二:方式三csrf添加装饰器的多种方式FBV中添加装饰器的方式CBV添加的方式方式一方式......
  • 第三章 --------------------XAML的属性和事件
    1.XAML注释是什么样子的? 在之前的章节有提起过,但是这一节我还是想系统的学习XAML,XAML的注释如下<!--     //这其中填写注释-->Notice:在注释的部分编译器......
  • 线程之间的同步和互斥
                          ......
  • 高斯消元+组合数+卡特兰数
    高斯消元+组合数+卡特兰数高斯消元\(O(n^3)\)的线性时间内求解n元线性方程组\[\\\begin{cases}\a_{11}x_1+a_{12}x_2+...+a_{1n}x_n=b_1\\\a_{21}x_1+a_{22}x_2+.......
  • Safari浏览器对SVG中的<foreignObject>标签支持不友好,渲染容易错位
    在svg中需要写一个markdown编辑器,需要用到<foreignObject>绘制来html,编辑器选择了simplemde。大致html部分结构如下,<markdown-editor>组件为定制封装好的simp......
  • 加密解决HTTP协议带来的安全问题
    HTTP协议默认是采取明文传输的,容易被中间人窃听、拦截、篡改,存在安全隐患。常见提高安全性的方法是对通信内容进行加密,再进行传输,常见的加密方式有不可逆加密:单向散列......
  • 工作一些常用的宏定义
    前言​ 在C语言的编程中,宏定义是用宏名来表示一个字符串,在宏展开时又以该字符串取代宏名,这只是一种简单的替换。在实践中阅读他人的代码和自己编写代码中,如果可以灵活的......
  • 2022~2023
    新年快乐  我用什么才能留住你?  我给你贫穷的街道、绝望的日落、破败郊区的月亮。  我给你一个久久地望着孤月的人的悲哀。——博尔赫斯   ......
  • leetcode 451. 根据字符出现频率排序
    一、题目给定一个字符串s,根据字符出现的频率对其进行降序排序。一个字符出现的频率是它出现在字符串中的次数。返回已排序的字符串 。如果有多个答案,返回其中......
  • openWRT相关
    OpenWrt固件下载https://openwrt.club/dleSir的x86固件高大全版https://drive.google.com/drive/folders/1PsS3c0P7a4A4KY8plQg4Fla8ZI-PGBb1 OpenWrtDownloadsWelcom......