请问在VC怎么可以用C++把图片旋转90度

fengye_1998 2017-02-13 10:53:35
加精
小白求教~
...全文
5941 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
NoEdUl 2017-02-23
  • 打赏
  • 举报
回复
。。。。。感觉看明白楼上一堆接口的时间差不多可以写一个函数了... 完了我这不喜欢看别人代码的毛病要影响到职业发展了/
mailwjl 2017-02-22
  • 打赏
  • 举报
回复
没有太看明白问题
菅田将晖丶 2017-02-22
  • 打赏
  • 举报
回复
百度挺多的a
皮皮00000000 2017-02-22
  • 打赏
  • 举报
回复
最简单 opencv 有旋转的API
阿源是少年 2017-02-21
  • 打赏
  • 举报
回复
用CxImage,这个库需要自己去下载源码,百度一搜就好 CxImage* pImage = new CxImage; pImage->Load("图片路径"); pImage->Rotate(...) pImage->Draw(...)
赵4老师 2017-02-20
  • 打赏
  • 举报
回复
仅供参考:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
wchar_t formats[5][11]={
    L"image/bmp",
    L"image/jpeg",
    L"image/gif",
    L"image/tiff",
    L"image/png",
};
wchar_t exts[5][5]={
    L".bmp",
    L".jpg",
    L".gif",
    L".tif",
    L".png",
};
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   ImageCodecInfo* pImageCodecInfo = NULL;
   GetImageEncodersSize(&num, &size);
   if(size == 0) return -1;  // Failure
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL) return -1;  // Failure
   GetImageEncoders(num, size, pImageCodecInfo);
   for (UINT j = 0; j < num; ++j) {
      if ( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
int wmain(int argc,wchar_t *argv[]) {
    int r=1;
    if (argc<4) {
    USAGE:
        wprintf(L"%s srcimg.{bmp|jpg|gif|tif|png|wmf|emf|ico}  desimg.{bmp|jpg|gif|tif|png}  angle\n",argv[0]);
        return r;
    }
    int i;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[1]+wcslen(argv[1])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[2]+wcslen(argv[2])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
    {
        Image img(argv[1]);
        if (Ok==img.GetLastStatus()) {
            UINT height = img.GetHeight();
            UINT width  = img.GetWidth();
            REAL angle;
            if (1==swscanf_s(argv[3],L"%f",&angle)) {
                REAL size;
                size=(REAL)sqrt(1.0*width*width+1.0*height*height);
                Matrix mat;
                mat.Translate(size / -2.0f, size / -2.0f);
                mat.Rotate(-angle, MatrixOrderAppend);
                mat.Translate(size / 2.0f, size / 2.0f, MatrixOrderAppend);
                PointF pfTL((size-width)/2.0f      ,(size-height)/2.0f       );
                PointF pfTR((size-width)/2.0f+width,(size-height)/2.0f       );
                PointF pfBL((size-width)/2.0f      ,(size-height)/2.0f+height);
                PointF pfBR((size-width)/2.0f+width,(size-height)/2.0f+height);
                Graphics tgp(&img);
                Bitmap bmp((UINT)size,(UINT)size,&tgp);//Let bmp Resolution equal to img Resolution
                Graphics gp(&bmp);
                gp.SetTransform(&mat);
                gp.DrawImage(&img,pfTL);
                REAL xmin,ymin,xmax,ymax,x,y,rw,rh;
                mat.TransformPoints(&pfTL);
                xmin=xmax=pfTL.X;
                ymin=ymax=pfTL.Y;
                mat.TransformPoints(&pfTR);
                if (xmin>pfTR.X) xmin=pfTR.X;
                if (xmax<pfTR.X) xmax=pfTR.X;
                if (ymin>pfTR.Y) ymin=pfTR.Y;
                if (ymax<pfTR.Y) ymax=pfTR.Y;
                mat.TransformPoints(&pfBL);
                if (xmin>pfBL.X) xmin=pfBL.X;
                if (xmax<pfBL.X) xmax=pfBL.X;
                if (ymin>pfBL.Y) ymin=pfBL.Y;
                if (ymax<pfBL.Y) ymax=pfBL.Y;
                mat.TransformPoints(&pfBR);
                if (xmin>pfBR.X) xmin=pfBR.X;
                if (xmax<pfBR.X) xmax=pfBR.X;
                if (ymin>pfBR.Y) ymin=pfBR.Y;
                if (ymax<pfBR.Y) ymax=pfBR.Y;
                x=xmin;
                y=ymin;
                rw=xmax-x;
                rh=ymax-y;
                Bitmap* clone;
                clone = bmp.Clone(x,y,rw,rh,PixelFormat24bppRGB);//bmp.GetPixelFormat()
                CLSID encoderClsid;
                if (0<=GetEncoderClsid(formats[i],&encoderClsid)) {
                    if (Ok==clone->Save(argv[2],&encoderClsid)) {
                        wprintf(L"OK to %s  %s  %s  %s\n",argv[0],argv[1],argv[2],argv[3]);
                        r=0;
                    } else {
                        wprintf(L"Error to save %s\n",argv[2]);
                        r=4;
                    }
                } else {
                    wprintf(L"Error to GetEncoderClsid(%s,...)\n",formats[i]);
                    r=3;
                }
                delete clone;
            } else {
                wprintf(L"Error to get angle %s\n",argv[3]);
                r=2;
            }
        } else {
            wprintf(L"Error to load %s\n",argv[1]);
            r=5;
        }
    }
    GdiplusShutdown(gdiplustoken);
    return r;
}
fengye_1998 2017-02-17
  • 打赏
  • 举报
回复
引用 12 楼 schlafenhamster 的回复:
6楼 GDI+ 例子对 90度 旋转 最简单
我没学过
ljheee 2017-02-16
  • 打赏
  • 举报
回复
shiter 2017-02-15
  • 打赏
  • 举报
回复
已经推荐,谁来把效果贴一下,哈哈
qq_17619515 2017-02-15
  • 打赏
  • 举报
回复
用GDI+可以啊
schlafenhamster 2017-02-15
  • 打赏
  • 举报
回复
5 楼 GDI+ 10度旋转。 deg += 10.0f;
schlafenhamster 2017-02-15
  • 打赏
  • 举报
回复
6楼 GDI+ 例子对 90度 旋转 最简单
fengye_1998 2017-02-15
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:

#define R 20     // radius
#define K 1.732f // sqr(3)
#define A 60     // degree
void GdipRotate(HDC hdc)
{
static float deg=0.0;
	Graphics graphics(hdc);
	Pen pen(Color(255, 0, 0, 255),2.0f);
//
	Matrix matrix;// identity matrix
#if 0 // 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
	REAL  m[6];
	matrix.GetElements(m);
	afxDump << m[0] << " m11\n";// 1.0
	afxDump << m[1] << " m12\n";// 0
	afxDump << m[2] << " m21\n";// 0
	afxDump << m[3] << " m22\n";// 1.0
	afxDump << m[4] << " dx\n"; // 0
	afxDump << m[5] << " dy\n"; // 0
#endif 
	PointF Center(200, 200);
	matrix.RotateAt(deg, Center , MatrixOrderPrepend);// matrix is on the right
	PointF pt0(Center.X,80);// arrow
	PointF pt1(Center.X-R/2,Center.Y-K*R/2);
	PointF pt2(Center.X+R/2,Center.Y-K*R/2);
	graphics.SetTransform(&matrix);
// ^ =-90 ; -> = 0
	RectF rc(Center.X-R,Center.Y-R, 2*R, 2*R);
    graphics.DrawArc(&pen,rc,-A,360-A);
    graphics.DrawLine(&pen,pt1,pt0);
    graphics.DrawLine(&pen,pt2,pt0);
	deg += 10.0f;
}
deg += 10.0f;
大神,我没看懂..好似很多函数都没定义
fengye_1998 2017-02-15
  • 打赏
  • 举报
回复
引用 9 楼 chenlycly 的回复:
用GDI+最简单了!
可是我不会T T
dvlinker 2017-02-15
  • 打赏
  • 举报
回复
用GDI+最简单了!
Anhaoxu 2017-02-15
  • 打赏
  • 举报
回复
opencv很容易做到
一条晚起的虫 2017-02-15
  • 打赏
  • 举报
回复
// GDI+
ooolinux 2017-02-15
  • 打赏
  • 举报
回复
引用 1 楼 u010165006 的回复:
C++ Builder利用Canvas->Pixels[x][y]把图片旋转90度示例程序
http://blog.163.com/tab_98/blog/static/11924097201711395726948/

下载:
http://download.csdn.net/detail/u010165006/9753094


图片上传失败(很久以来都这样了,圆圈转不停就是上传不了)
其他楼兄弟传吧。
worldy 2017-02-14
  • 打赏
  • 举报
回复
使用SetTransform设置旋转矩阵,然后调用Bitblt
schlafenhamster 2017-02-14
  • 打赏
  • 举报
回复

BOOL CPicture::Load(LPCTSTR pszPathName)
{
	Free();
	USES_CONVERSION;
	m_pImage = Image::FromFile(A2W(pszPathName),
		m_bUseEmbeddedColorManagement);
	return m_pImage->GetLastStatus()==Ok;
}
、、
//////////////////
// Rotate image 
//
void CPicture::Rotate(RotateFlipType rft)
{
	if (m_pImage)
	{
		m_pImage->RotateFlip(rft);// Rotate90FlipNone
	}
}

加载更多回复(4)

19,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧