Added some Greenshot 1.1 code, is currently unused.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2000 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-08-21 12:15:21 +00:00
parent 8305327db5
commit d8374df331
2 changed files with 69 additions and 1 deletions

View file

@ -301,13 +301,17 @@ namespace Greenshot.Drawing {
} }
} }
public void SaveElementsToStream(Stream streamWrite) { public long SaveElementsToStream(Stream streamWrite) {
long bytesWritten = 0;
try { try {
long lengtBefore = streamWrite.Length;
BinaryFormatter binaryWrite = new BinaryFormatter(); BinaryFormatter binaryWrite = new BinaryFormatter();
binaryWrite.Serialize(streamWrite, elements); binaryWrite.Serialize(streamWrite, elements);
bytesWritten = streamWrite.Length - lengtBefore;
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Error serializing elements to stream.", e); LOG.Error("Error serializing elements to stream.", e);
} }
return bytesWritten;
} }
public void LoadElementsFromStream(Stream streamRead) { public void LoadElementsFromStream(Stream streamRead) {

View file

@ -31,6 +31,7 @@ using Greenshot.Forms;
using Greenshot.Plugin; using Greenshot.Plugin;
using GreenshotPlugin.Core; using GreenshotPlugin.Core;
using Greenshot.IniFile; using Greenshot.IniFile;
using Greenshot.Drawing;
namespace Greenshot.Helpers { namespace Greenshot.Helpers {
/// <summary> /// <summary>
@ -161,6 +162,69 @@ namespace Greenshot.Helpers {
} }
} }
/// <summary>
/// Save a Greenshot surface
/// </summary>
/// <param name="surface">Surface to save</param>
/// <param name="fullPath">Path to file</param>
public static void SaveGreenshotSurface(Surface surface, string fullPath) {
fullPath = FilenameHelper.MakeFQFilenameSafe(fullPath);
string path = Path.GetDirectoryName(fullPath);
// Get output settings from the configuration
OutputSettings outputSettings = new OutputSettings(OutputFormat.png);
// check whether path exists - if not create it
DirectoryInfo di = new DirectoryInfo(path);
if (!di.Exists) {
Directory.CreateDirectory(di.FullName);
}
using (FileStream stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) {
SaveToStream(surface.Image, stream, outputSettings);
long bytesWritten = surface.SaveElementsToStream(stream);
using (BinaryWriter writer = new BinaryWriter(stream)) {
writer.Write(bytesWritten);
}
}
}
/// <summary>
/// Load a Greenshot surface
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
public static Surface LoadGreenshotSurface(string fullPath) {
Surface returnSurface = null;
if (string.IsNullOrEmpty(fullPath)) {
return null;
}
Bitmap fileBitmap = null;
LOG.InfoFormat("Loading image from file {0}", fullPath);
// Fixed lock problem Bug #3431881
using (Stream imageFileStream = File.OpenRead(fullPath)) {
// And fixed problem that the bitmap stream is disposed... by Cloning the image
// This also ensures the bitmap is correctly created
// We create a copy of the bitmap, so everything else can be disposed
imageFileStream.Position = 0;
using (Image tmpImage = Image.FromStream(imageFileStream, true, true)) {
LOG.DebugFormat("Loaded {0} with Size {1}x{2} and PixelFormat {3}", fullPath, tmpImage.Width, tmpImage.Height, tmpImage.PixelFormat);
fileBitmap = ImageHelper.Clone(tmpImage);
}
returnSurface = new Surface(fileBitmap);
imageFileStream.Seek(-8, SeekOrigin.End);
long bytesWritten = 0;
using (BinaryReader reader = new BinaryReader(imageFileStream)) {
bytesWritten = reader.ReadInt64();
imageFileStream.Seek(-(bytesWritten+8), SeekOrigin.End);
returnSurface.LoadElementsFromStream(imageFileStream);
}
}
if (fileBitmap != null) {
LOG.InfoFormat("Information about file {0}: {1}x{2}-{3} Resolution {4}x{5}", fullPath, fileBitmap.Width, fileBitmap.Height, fileBitmap.PixelFormat, fileBitmap.HorizontalResolution, fileBitmap.VerticalResolution);
}
return returnSurface;
}
/// <summary> /// <summary>
/// Saves image to specific path with specified quality /// Saves image to specific path with specified quality
/// </summary> /// </summary>