100分求教:怎样用VC编写一个程序,能实现读取一个BMP图片,改变大小后,保存为新的BMP图片!

brave_yh 2007-08-13 05:08:50
要求将352*288象素的BMP图片变成288*176的!
...全文
1040 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
han856914 2010-12-29
  • 打赏
  • 举报
回复
功能很强大。
frankieswan 2007-08-16
  • 打赏
  • 举报
回复
/*************************************/
CShrink.h
/***************************************/
#if !defined(AFX_SHRINKER_H__9C214094_0019_4DF9_AC6E_9E22F34E70D6__INCLUDED_)
#define AFX_SHRINKER_H__9C214094_0019_4DF9_AC6E_9E22F34E70D6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Shrinker.h : header file


typedef struct {
WORD x,y; // dimensions
WORD l; // bytes per scan-line (32-bit allignment)
BYTE *b; // bits of bitmap,3 bytes/pixel, BGR
} tWorkBMP; // 24-bit working bitmap*/

typedef struct tagpels1{
BYTE bred;
BYTE bgreen;
BYTE bblue;
}pels3;

typedef struct tagpels2{
BYTE data1;
BYTE data2;
}pels2;

//#define SHRBPATH "./pic_temp/shrbmp.bmp"
#define Alloc(p,t) (t *)malloc((p)*sizeof(t))
#define For(i,n) for ((i)=0;(i)<(n);(i)++)
#define iFor(n) For (i,n)
#define jFor(n) For (j,n)


class CShrinker
{
public:
CShrinker();
~CShrinker();
private:
WORD shrwith; //缩放宽
WORD shrhith; //缩放长
BITMAPFILEHEADER bf; //BMP 文件头
BITMAPINFOHEADER bmpHeader;//BMP 信息头
public:
// Operation
// HBITMAP CreateEmptyBitmap (WORD dx,WORD dy);
void SaveWorkingBitmap (tWorkBMP *w,HBITMAP b);
void ShrinkWorkingBitmap (tWorkBMP *a,tWorkBMP *b,WORD bx,WORD by);
void CreateBitmap (WORD dx,WORD dy,tWorkBMP *w);
//void SetBMIHeader (BITMAPINFO *b,short dx,short dy);
BOOL ShrinkPath(char *srcpath, char *destpath,WORD pwith,WORD phith,BYTE *content);
void CreateWorkingBitmap (WORD dx,WORD dy,tWorkBMP *w);
BOOL SaveOutFile(char *BmpFileName1,tWorkBMP *out);
//CString FindExtension(const CString& name);
//int FindFormat(const CString& ext);
public:


};


#endif


/***************************************
CShrink.cpp
/****************************************/
// Shrinker.cpp : implementation file
//

#include "stdafx.h"
#include "Shrinker.h"

CShrinker::CShrinker()

{
shrwith=128;
shrhith=128;

}

CShrinker::~CShrinker()
{
}

void CShrinker::CreateBitmap (WORD dx,WORD dy,tWorkBMP *w)
{
w->x=dx;
w->y=dy;
w->l=(dx+1)*3&0xfffc;
}

void CShrinker::CreateWorkingBitmap(WORD dx,WORD dy,tWorkBMP *w)
{
w->x=dx;
w->y=dy;
w->l=(dx+1)*3&0xfffc;
w->b=Alloc(w->l*dy,BYTE);
}

//**************************************/
/*这里只针对24位真采,即3个BYTE表示一个
/*象素的情况
/*bmppath: 要进行缩放的BMP文件
//**************************************/
BOOL CShrinker::ShrinkPath(char *srcpath, char *destpath,WORD pwith,WORD phith,BYTE *content )
{
long With;
long High;
WORD bx,by;
tWorkBMP in,out;
pels3 *image_content;
HFILE hf;
if((hf=_lopen(srcpath,OF_READ))==HFILE_ERROR) {
AfxMessageBox("导入文件错误");
return FALSE;
}
_lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lread(hf,(LPSTR)&bmpHeader,sizeof(BITMAPINFOHEADER));
With=bmpHeader.biWidth;
High=bmpHeader.biHeight;
CreateBitmap(bmpHeader.biWidth,bmpHeader.biHeight,&in);
image_content=(pels3 *)malloc(With*High*3);
in.b=(BYTE *)image_content;
_lread(hf,(LPSTR)(in.b),With*High*3);
ShrinkWorkingBitmap(&in,&out,pwith,phith);
memcpy(content,out.b,pwith*phith*3);
SaveOutFile(destpath,&out);
free(image_content);
free(out.b);
return TRUE;
}

void CShrinker::ShrinkWorkingBitmap (tWorkBMP *a,tWorkBMP *b,WORD bx,WORD by)
{
BYTE *uy=a->b,*ux,i;
WORD x,y,nx,ny=0;
DWORD df=3*bx,nf=df*by,j;
float k,qx[2],qy[2],q[4],*f=Alloc(nf,float);

CreateWorkingBitmap (bx,by,b);

jFor (nf) f[j]=0;
j=0;

For (y,a->y) {
ux=uy;
uy+=a->l;
nx=0;
ny+=by;

if (ny>a->y) {

qy[0]=1-(qy[1]=(ny-a->y)/(float)by);

For (x,a->x) {

nx+=bx;

if (nx>a->x) {
qx[0]=1-(qx[1]=(nx-a->x)/(float)bx);

iFor (4) q[i]=qx[i&1]*qy[i>>1];

iFor (3) {
f[j]+=(*ux)*q[0];
f[j+3]+=(*ux)*q[1];
f[j+df]+=(*ux)*q[2];
f[(j++)+df+3]+=(*(ux++))*q[3];
}
}
else iFor (3) {
f[j+i]+=(*ux)*qy[0];
f[j+df+i]+=(*(ux++))*qy[1];
}
if (nx>=a->x) nx-=a->x;
if (!nx) j+=3;
}
}
else {
For (x,a->x) {

nx+=bx;

if (nx>a->x) {
qx[0]=1-(qx[1]=(nx-a->x)/(float)bx);
iFor (3) {
f[j]+=(*ux)*qx[0];
f[(j++)+3]+=(*(ux++))*qx[1];
}
}
else iFor (3) f[j+i]+=*(ux++);

if (nx>=a->x) nx-=a->x;
if (!nx) j+=3;
}
if (ny<a->y) j-=df;
}
if (ny>=a->y) ny-=a->y;
}

nf=0;
k=bx*by/(float)(a->x*a->y);
uy=b->b;

For (y,by) {
jFor (df) uy[j]=f[nf++]*k+.5;
uy+=b->l;
}

free (f);
}


BOOL CShrinker::SaveOutFile(char *BmpFileName1,tWorkBMP *out)
{
HFILE hf1;
BYTE buf1[14];
memset(buf1,6,14);

if((hf1=_lcreat(BmpFileName1,OF_CREATE))==HFILE_ERROR) {
AfxMessageBox("fali");
return FALSE;
}
bmpHeader.biWidth=shrwith;
bmpHeader.biHeight=shrhith;
_lwrite(hf1,(LPSTR)&bf,14);
_lwrite(hf1,(LPSTR)&bmpHeader,sizeof(bmpHeader));
_lwrite(hf1,(LPSTR)out->b,128*128*3);
//AfxMessageBox("缩放成功");
return true;
}
cangzhu 2007-08-15
  • 打赏
  • 举报
回复
StretchBlt

不行吗?

还是图象处理的算法??
guohua219 2007-08-14
  • 打赏
  • 举报
回复
用vc吧,就是图象缩放的问题哈~~~
主要用StretchBlt函数~~
当然前面你得弄清如何打开个图象~~
smallfishff 2007-08-14
  • 打赏
  • 举报
回复
基础类中已回答你问题了 还是说 如果外挂程序 用VB比较适合.
emptyness 2007-08-14
  • 打赏
  • 举报
回复
接分...百度..
圆圆木公 2007-08-14
  • 打赏
  • 举报
回复
http://download.csdn.net/source/172443这里有代码下载的
jun_01 2007-08-13
  • 打赏
  • 举报
回复
核心函数:StretchBlt

剩下的步骤也不难,去找找资料吧,如果用gdi+就更简单了。

ps,如果用c#,估计10行左右的代码就可以完成。vc如果不用gdi+估计得100行。
海鸥先生 2007-08-13
  • 打赏
  • 举报
回复
还有这个:
http://dev.yesky.com/164/2291664.shtml
海鸥先生 2007-08-13
  • 打赏
  • 举报
回复
希望这个对你有帮助:
http://mti.xidian.edu.cn/multimedia/multi/course1-6-1.html

19,468

社区成员

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

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