From 45647dcf1059cc6515a0c6157f7c077cfc2abd31 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sun, 17 Mar 2002 11:35:32 +0000 Subject: [PATCH] Chris' wxImage::SaveFile(filename_only) patch git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@14655 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 3 ++- include/wx/image.h | 1 + samples/image/image.cpp | 42 ++++++++++++++++++----------------------- src/common/image.cpp | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 2886d3368a..9c684d2142 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -118,6 +118,7 @@ All (GUI): - added wxGetFontFromUser() convenience function - added EVT_MENU_OPEN and EVT_MENU_CLOSE events - added Hungarian translations (Janos Vegh) +- added wxImage::SaveFile(filename) method (Chris Elliott) wxMSW: @@ -125,7 +126,7 @@ wxMSW: - refresh the buttons properly when the window is resized (Hans Van Leemputten) - huge (40*) speed up in wxMask::Create() - changing wxWindows styles also changes the underlying Windows window style -- wxTreeCtrl supports wxTR_HIDE_ROOT stle (George Policello) +- wxTreeCtrl supports wxTR_HIDE_ROOT style (George Policello) - fixed flicker in wxTreeCtrl::SetItemXXX() - fixed redraw problems in dynamically resized wxStaticText - improvements to wxWindows applications behaviour when the system colours diff --git a/include/wx/image.h b/include/wx/image.h index cf69aa743a..55e9d96834 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -187,6 +187,7 @@ public: virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ); #endif + virtual bool SaveFile( const wxString& name ) const; virtual bool SaveFile( const wxString& name, int type ) const; virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const; diff --git a/samples/image/image.cpp b/samples/image/image.cpp index c0668f2331..60dac7ff8d 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -183,34 +183,28 @@ public: delete cmap; } - bool saved = FALSE; - + bool loaded; wxString extension = savefilename.AfterLast('.').Lower(); - if (extension == "bmp") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_BMP); - else if (extension == "png") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_PNG); - else if (extension == "pcx") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_PCX); - else if ((extension == "tif") || (extension == "tiff")) - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_TIF); - else if (extension == "jpg") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_JPEG); - else if (extension == "pnm") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_PNM); - else if (extension == "ico") - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_ICO); - else if (extension == "cur") - { + if (extension == "cur") + { image.Rescale(32,32); image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); - image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); - saved=image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); - } - else - wxMessageBox("Unknown file type, see options in file selector.", - "Unknown file type", + image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); + // This shows how you can save an image with explicitly + // specified image format: + loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); + } + else + { + // This one guesses image format from filename extension + // (it may fail if the extension is not recognized): + loaded = image.SaveFile(savefilename); + } + + if ( !loaded ) + wxMessageBox("No handler for this file type.", + "File was not saved", wxOK|wxCENTRE, this); } diff --git a/src/common/image.cpp b/src/common/image.cpp index 2c5ba9758d..9d5716221c 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -938,6 +938,24 @@ bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int #endif // wxUSE_STREAMS } + + +bool wxImage::SaveFile( const wxString& filename ) const +{ + wxString ext = filename.AfterLast('.').Lower(); + + wxImageHandler * pHandler = FindHandler(ext, -1); + if (pHandler) + { + SaveFile(filename, pHandler->GetType()); + return TRUE; + } + + wxLogError(_("Can't save image to file '%s': unknown extension."), filename.c_str()); + + return FALSE; +} + bool wxImage::SaveFile( const wxString& filename, int type ) const { #if wxUSE_STREAMS -- 2.47.2