Hiện tại trong C (Console) chưa thể đưa ảnh vào được. Cậu cũng ko cần màu mè gì. Học lên chút thì sẽ làm sao? Dễ mà
Xin hỏi làm thế nào để đưa ảnh từ bên ngoài vào chương trình của mình ??
Hiện tại trong C (Console) chưa thể đưa ảnh vào được. Cậu cũng ko cần màu mè gì. Học lên chút thì sẽ làm sao? Dễ mà
Hix, ko có cách nào thật sao?
ừ...! có lẽ thế, bởi mình cũng chưa được biết các chèn hình mờ... cứ như bác kidkid đó màu mè làm chi cho mệt người...^_^
Đừng nói với tôi vấn đề đó khó...nếu nó không khó nó không phải là vấn đề....‰ —» % —» ¼ —» ½ —» 1 :Cái đích của ước mơ
.´¯).:.___.:.(¯` ly_chank_ho ´¯).:.___.:.(¯`.
Uh. Mình đang làm BT lớn là viết 1 game nên định lấy ít hình bên ngoài vào chứ vẽ trong C thì xấu ẹ. Thế này thì chán quá. (D
ke tớ gặp ai cũng nghe toàn bài tập lớn, bài tập bự hết, nhưng chả biết bự cỡ nào. 1 khi là 1 project để kiếm tiền thì ít khi đòi hỏi đồ họa trên C . Còn nếu là để kiếm điểm thì chẳng thầy cô nào lại bắt bẻ điều phi lí. Ngoại trừ các gv của cậu yêu cầu cậu phần thư viện graphic. Nếu cậu mới bắt đầu, thì bài tập của cậu sẽ được chấm điểm ở giải thuật, phong cách lập trình, ý tưởng... và cuối cùng là công dụng.
Thế nhé.
Anh dùng c for dos đúng ko, thế thì vào chế độ đồ họa, chèn ảnh cũng đc. mà !!
Life:\> dir
Để đưa ảnh vào chương trình viết bằng C for DOS bạn cần sử dụng thư viện đồ họa với svga256.bgi. Trong diễn đàn đã có đưa lên file này rồi đó.
Sau đây là một ví dụ cho phép bạn load một hình ảnh dạng bitmap vào chương trình.
PHP Code://Bitmap struct
struct A{
char type[2]; /* Magic identifier */
unsigned long size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned long offset; /* Offset to image data, bytes */
};
extern A HEADER,HEADER1;
struct B{
unsigned long size; /* Header size in bytes */
unsigned long width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned long compression; /* Compression type */
unsigned long imagesize; /* Image size in bytes */
unsigned long xresolution,yresolution; /* Pixels per meter */
unsigned long ncolours; /* Number of colours */
unsigned long importantcolours; /* Important colours */
};
extern B INFOHEADER,INFOHEADER1;
PHP Code:huge DetectSvga()
{
return 3; //You can set the return value either 1,2 or 3. For me 3 is the best combination.
}
PHP Code:void Show()
{
fstream File;
//Here you have to define the path of the bitmap file. Like according to this example i have to open one Board1.bmp file. So write you bitmap file path here.
File.open("d:\\TEMP\\Temp.bmp",ios::in);
unsigned char Ch;
File.read((char*)&HEADER,14); //This is the header part of the Bitmap. It always looks like same. Don't change the content hear. The value remains 14 here.
File.read((char*)&INFOHEADER,40); //This is another part of the bitmap, here also the value remains same like 40 here.
unsigned int i;
char ColorBytes[4];
char*PaletteData;
PaletteData=new char[256*3];
if(PaletteData)//if memory allocated successfully
{
//read color data
for(i=0;i<256;i++)
{
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
outp(0x03c8,0); //tell DAC that data is coming
for(i=0;i<256*3;i++) //send data to SVGA DAC
{
outp(0x03c9,PaletteData[i]);
}
delete[]PaletteData;
}
for(i=0;i<INFOHEADER.height;i++) //This for loop is used to display the bitmap.
{
for(int j=0;j<INFOHEADER.width;)
{
File.read(&Ch,1); // Here Ch reads the color of your bitmap.
putpixel(XCor+j++,YCor+INFOHEADER.height-i-1,Ch); //XCor and YCor are the X and Y cordinates. It depends upon you.
}
}
File.close();
}
Cố nghiên cứu nhé, trên đây là đầy đủ các hàm cần thiết để có thể load một hình ảnh dạng bitmap vào chương trình viết bằng C for DOS.PHP Code:void main()
{
int gd = DETECT, md, a;
installuserdriver("SVGA256",&DetectSvga);
initgraph(&gd,&md,"D:\\BORLANDC\\bgi"); //Path may be different in your computer.
Show();
}
Nếu bạn muốn load hình ảnh loại khác cần thay đổi lại cấu trúc của loại ảnh đó.
Một người nào đó coi thường ý thức kỷ luật cũng có nghĩa là người đó đã coi thường tương lai số phận của chính bản thân người đó. Những người coi thường ý thức kỷ luật sẽ không bao giờ có được sự thành công trong sự nghiệp!Email: admin[@]congdongcviet.com | CC to: info[@]congdongcviet.com
Phone: 0972 89 7667 (Office: 04 6329 2380)
Yahoo & Skype: dreaminess_world (Vui lòng chỉ rõ mục đích ngay khi liên hệ, cảm ơn!)
cái này lượm trên mạng nè
PHP Code:ifstream fp;
void loadbmp(char *name,int offset, int w, int h);
void readdata(char *name)
{
cleardevice();
fp.open(name);
if (!fp) {
fp.close();
setcolor(RED);
cout<<"Error Opening file "<<name<<"\nMake sure all the required Files are in the same directory and they are not in use.\t";cout<<"\n\t\tPress enter to exit.";
getche();
exit(1);
}
int data1;
long data2;
int type;
fp.read((char*)&type,sizeof(type));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
int offset=data2;
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
int w=data2;
fp.read((char*)&data2,sizeof(data2));
int h=data2;
fp.read((char*)&data1,sizeof(data1));
fp.read((char*)&data1,sizeof(data1));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
fp.read((char*)&data2,sizeof(data2));
loadbmp(name,offset,w,h);
fp.close();
}
//~loadbmp(){};
//};
void loadbmp(char *name,int offset, int w, int h) {
char data3;
fp.seekg(offset);
int a=w;
if(w%8!=0)
a=(w+8)-(w%8);
int count=0,row=h;
long pos;
while(row>0 && count<a)
{
while(!fp.eof())
{
fp.read(&data3 , sizeof(data3));
gotoxy(150,120);
putpixel(count , row , ((data3 & 0xf0)>>4));
count++;
gotoxy(150,120);
putpixel(count , row , data3 & 0x0f);
count++;
if(count>=a)
{
count=0;
row-=1;
}
}
pos=fp.tellg();
fp.close();
fp.open(name);
count++;
if(count==a)
count=0;
count++;
if(count==a)
count=0;
fp.seekg(pos+2);
}
// return 0;
}
Thanks các bác nhiều!