Click here to Skip to main content
15,881,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to rotate a 686*800 bmp photo 90 degrees.
here's my code, it corrupts the file!

C#
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    fstream flower("flower.bmp", ios::in | ios::out | ios::binary);
    flower.seekg(18,ios::beg);
    char read_arr[4];
    char read_arr1[4];
    flower.read(read_arr,4);
    flower.read(read_arr1,4);
    flower.seekg(22,ios::beg);
    flower.write(read_arr,4);
    flower.seekg(18,ios::beg);
    flower.write(read_arr1,4);

    flower.seekg(54,ios::beg);
    char temp1[3];
    char temp2[3];

int j=0;
for(int k=0;k<=800;k++)
{
    for(int i=0;i<=686;i++)
    {
        flower.seekg((54+(3*i)+(3*j)),ios::beg);
        flower.read(temp1,3);
        flower.seekg(54+800*3*(685-i),ios::beg);
        flower.write(temp1,3);

    }
    j+=686;
}
    flower.close();
}
Posted
Comments
barneyman 23-Feb-15 18:49pm    
you're reading and writing into the same file, you're overwriting data you haven't read yet

it would be much more efficient to create an in-memory array/buffer, read a row from file, write a column to memory, then when done, dump the array to file
Alexis i 23-Feb-15 18:55pm    
ugh, that's it thank you very much

1 solution

as barneyman wrote rotating the bmp in memory is the right solution.

Read the outstanding article CXImage which provides some tons of features and also rotating code. In the sources you will find some code for rotating images.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900