|
Hello there.
I am writing picture editor and I have a problem with saving images to the same file.
Of course I can save them to the different file names, but not to the original filename.
So I've searched and found a page where are couple of steps that I have to do.
1. Open the image file
2. create a temporary in-memory image the same size as the original
3. obtain a Graphics object for the temporary image
4. Draw the original image onto the temporary one
5. dispose of the original
6. do any drawing you'd like on the image using the Graphics you have
7. Dispose of the Graphics
8. Write the temporary file to any filename and any format you please including the same name and format if you wish.
The idea is clear to me, and I know what I have to do, but I have one problem.
There is no such function as Dispose() in GDI+. It is in MFC of course, but not in GDI and I don't know how to "Close the file" so I can save the image under the same filename.
I've tried CloseHandle(HANDLE) function, but it seems it doesn't work.
Can anybody help me with this ?
soyo
|
|
|
|
|
FileStream fs = new FileStream("Sunset.jpg", FileMode.Open);
Bitmap b = new Bitmap(fs);
fs.Close();
Graphics.FromImage(b).FillRectangle(Brushes.Black, 0,0,b.Width, b.Height);
fs = new FileStream("Sunset.jpg", FileMode.Truncate);
b.Save(fs, System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0], null );
fs.Close();
"Until the day of his death, no man can be sure of his courage" -- Jean Anouilh
|
|
|
|
|
The Image class does have a Dispose() method, though I never use it, opting to just delete the Image (well usually Bitmap) object.
Do note that for some file formats GDI+ requires that the original stream remain intact. For others, it reads the data in entirely. My solution has been to do what you are doing except for step 8, there I've closed both and then either moved the new file on top of the original, with the flag to not prompt, or deleted the original and renamed the new one.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Dispose() is not available when I don't write in MFC. There is no such function in Image Class under GDI+ section.
How can I delete a file no matter what circumstances ?
For example according to MSDN I cannot use DeleteFile(LPCTSTR) to Read-Only files.
|
|
|
|
|
1980soyo wrote: Dispose() is not available when I don't write in MFC. There is no such function in Image Class under GDI+ section.
My mistake, I was just looking at MSDN documentation. As I said, I always delete the object directly or by letting it go out of scope.
1980soyo wrote: How can I delete a file no matter what circumstances ?
You can't. You can delete a file only if you have permissions to do so and it is not open. If a file is read-only, you can attempt to change the attributes using SetFileAttributes() , but that will work only if you have proper permission and/or security to do so.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Hi,
I am coding a program in VC++, mfc, in which I am using CString sql almost every where. Each time i use it, I have to declare. I tried declaring it in stdafx.h but I get errors
Budget.obj : error LNK2005: "class CString sqla" (?sqla@@3VCString@@A) already defined in Collect.obj
DeleteCat.obj : error LNK2005: "class CString sqla" (?sqla@@3VCString@@A) already defined in Collect.obj
StdAfx.obj : error LNK2005: "class CString sqla" (?sqla@@3VCString@@A) already defined in Collect.obj
Debug/sbh2.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
I checlked and rechecked a thousand times. I have not made multiple difinition. Then why is this happening?
I thought maybe it is because of 'sql' so I changed it to 'sqla', in vain.
Fortitudine Vinsinues!
|
|
|
|
|
Because this header has been included in the same module many times (maybe recursively)! Normally you can employ certain preprocessor directives to ensure the header only being included once, but in this case, you should avoid declaring variables in a header. An appropriate way is:
1) Declare a global variable in a source file (.cpp) only once:
CString sqla;
2) If any other source file in the same module needs to use this variable, use "extern" keword:
extern CString sqla;
Best,
Jun
|
|
|
|
|
I do not understand
If I declare CString sqla , say in Budget.cpp (a dialog class), then I have to declare it again as extern CString sqla in payroll.cpp (another dialog class)? Is that right
Actually what I am trying to do is declare CString sqla in just one place in the program and then use just sqla directly in any other .cpp file. Is that possible?
Usually, a dialog class has the .cpp file and .h file. If you want to use CString sqla in all the functions of that particular .cpp file, you declare it in the header file. But you cannot use sqla in another dialog class source. You have to declare it again. Is there a way in which I declare it somewhere only once?
Like for example, if math.h is used in many class, you usually #include it in stdAfx.h ...
I hope you can understand what I am trying to say!
-- modified at 15:26 Monday 26th June, 2006
|
|
|
|
|
TheinstruTara wrote: Actually what I am trying to do is declare CString sqla in just one place in the program and then use just sqla directly in any other .cpp file.
Unless you are assigning a value to the CString object that changes infrequently, why are you doing this? Why not just create an instance of the object at the point it is needed?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I am using the CString object, sql almost every where. I use it to assign an sql "SELECT * FROM...." . The sql changes in different places. And everytime I have to asign something, I have to declare CString sql. That is why I am trying to make it global, so that I do not have to declare it again and again.
Fortitudine Vinsinues!
|
|
|
|
|
TheinstruTara wrote: That is why I am trying to make it global, so that I do not have to declare it again and again.
Why? What's wrong with a variable being declared when it is needed? You're not saving anything by using a global variable instead. That way of thinking has not been relevant with Windows in a number of years.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Well..I am not trying to save anything.
I have to type CString sql; everytime I have to use it. I guess just a bit lazy... And if there is a better way of doing it, then why not!
I do know that if a variable can be declared as totally global. Just wasn't sure how.
I guess, we all try to find easier way of doing things.Some toothbrush company has come out with, 'automated toothbrush' Batery operated stuff...
Fortitudine Vinsinues!
|
|
|
|
|
Automated Tooth brush is alreay there...But there is no available medicine for lazyness till now...
Look here.
http://www.pricegrabber.com/search_attrib.php/page_id=796/popup1%255B%255D=20:1765/
Pls dont ask for a stuff which have food for u
|
|
|
|
|
While you can use the extern solution provided, a better solution for you would be to define that string in your string table and declare a CString object to use it when needed.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
TheinstruTara wrote: then I have to declare it again as extern CString sqla in payroll.cpp (another dialog class)?
Keyword "extern" indicates that this variable has been declared elsewhere in the same module and tells the compiler to use that declaration.
TheinstruTara wrote: If you want to use CString sqla in all the functions of that particular .cpp file, you declare it in the header file.
That may be what you have been doing, but it's not right. You don't declare varaibles in a header, you only put macros, constants, type definitions (that sort of things) there.
In essense, header is about definition not declaration. People include your header because they want to use your type definitions, not the variables in your module!
Hope this is clearer.
Best,
Jun
|
|
|
|
|
But then, suppose I want to access a value (say some
double area = 3.14*m_Edit_r*m_Edit_r) )
calculated in one dialog box class's .cpp file from another dialog box or just another class then how is it done.
What I usually do is declare double area in Dialog1.h , do the calulation somewhere in Dialog1.cpp
Then, in Dialog2.cpp I access it this way pDlg->area + somethng ... where pDlg is a CDialog1 object;
So i have been doing it wrong?
Fortitudine Vinsinues!
|
|
|
|
|
TheinstruTara wrote: So i have been doing it wrong?
Aside from declaring the data member public, no, you did it right. Those variables should be private and be given accessor methods, though.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
As per classes, the header defines a class, which in truns encapsulates some data as members. You're not declaring data in the header directly; the header still only contains type definition, which is class in this case.
TheinstruTara wrote: Then, in Dialog2.cpp I access it this way pDlg->area + somethng ... where pDlg is a CDialog1 object;
So i have been doing it wrong?
No. This actually explains the class definition well. Your Dialog2 accesses "area" through the 1st Dialog object "pDlg", which is most likely declared as a member of Dialog2 in your header.
Best,
Jun
|
|
|
|
|
Thanks.
In conclusion, if I am making a module, I must not do it the same way as I do it for classes. But for simple class, I can declare variables in the header.
Fortitudine Vinsinues!
|
|
|
|
|
Jun Du wrote: In essense, header is about definition not declaration.
Header (.h) files contain declarations, while source (.cpp) files contain definitions. Declarations can be seen multiple times by the compiler, while definitions can only be seen once by the compiler. A definition is also a declaration, but a declaration is not also a definition.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Header (.h) files contain declarations, while source (.cpp) files contain definitions.
I have a feeling that the source file (.cpp) contains class implementation, not definition. The header contains the definition.
Best,
Jun
|
|
|
|
|
Jun Du wrote: I have a feeling that the source file (.cpp) contains class implementation, not definition. The header contains the definition.
For creating your own class, yes, but the scope of this thread was limited to using an existing object.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
Thank you. That is what I was looking for.
Fortitudine Vinsinues!
|
|
|
|
|
I've looked and looked, but i can't seem to find any good articles on pointers....
its all so confusing to me....
I would appriecate if anyone could point (he he) me to a good article or something you read that you though was good on pointers and references..
It would help a lot thanks...
"C++ will solve any problem."
|
|
|
|