cleanup & small fixes. Added some more IBinaryContainer implementations which are used by plugins which aren't rolled out yet. (but I didn't want to loose the code)

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2524 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-03-07 10:08:57 +00:00
commit b7b89ade04
7 changed files with 167 additions and 38 deletions

View file

@ -56,6 +56,31 @@ namespace Greenshot.Helpers {
private ScreenCaptureMode screenCaptureMode = ScreenCaptureMode.Auto; private ScreenCaptureMode screenCaptureMode = ScreenCaptureMode.Auto;
private Thread windowDetailsThread = null; private Thread windowDetailsThread = null;
/// <summary>
/// The public accessible Dispose
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
/// <summary>
/// This Dispose is called from the Dispose and the Destructor.
/// When disposing==true all non-managed resources should be freed too!
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
// Cleanup
}
windows = null;
selectedCaptureWindow = null;
windowDetailsThread = null;
capture = null;
}
public static void CaptureClipboard() { public static void CaptureClipboard() {
new CaptureHelper(CaptureMode.Clipboard).MakeCapture(); new CaptureHelper(CaptureMode.Clipboard).MakeCapture();
} }

View file

@ -183,7 +183,7 @@ namespace Greenshot.Helpers {
/// after the form it is associated with. /// after the form it is associated with.
/// </summary> /// </summary>
~CopyData() { ~CopyData() {
Dispose(); Dispose(false);
} }
} }

View file

@ -66,11 +66,13 @@ namespace GreenshotDropboxPlugin {
// Try to get a URL to the uploaded image // Try to get a URL to the uploaded image
try { try {
string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null); string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null);
if (responseString != null) {
LOG.DebugFormat("Parsing output: {0}", responseString); LOG.DebugFormat("Parsing output: {0}", responseString);
IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString); IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString);
if (returnValues.ContainsKey("url")) { if (returnValues.ContainsKey("url")) {
return returnValues["url"] as string; return returnValues["url"] as string;
} }
}
} catch (Exception ex) { } catch (Exception ex) {
LOG.Error("Can't parse response.", ex); LOG.Error("Can't parse response.", ex);
} }

View file

@ -83,6 +83,9 @@ namespace GreenshotPicasaPlugin {
} }
public static string ParseResponse(string response) { public static string ParseResponse(string response) {
if (response == null) {
return null;
}
try { try {
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
doc.LoadXml(response); doc.LoadXml(response);

View file

@ -91,13 +91,16 @@ namespace GreenshotPlugin.Core {
/// the stream is checked if it's seekable and if needed a MemoryStream as "cache" is used. /// the stream is checked if it's seekable and if needed a MemoryStream as "cache" is used.
/// </summary> /// </summary>
/// <param name="imageToSave">image to save</param> /// <param name="imageToSave">image to save</param>
/// <param name="surface">surface for the elements, if the greenshot format is used</param> /// <param name="surface">surface for the elements, needed if the greenshot format is used</param>
/// <param name="stream">Stream to save to</param> /// <param name="stream">Stream to save to</param>
/// <param name="outputSettings">SurfaceOutputSettings</param> /// <param name="outputSettings">SurfaceOutputSettings</param>
public static void SaveToStream(Image imageToSave, ISurface surface, Stream stream, SurfaceOutputSettings outputSettings) { public static void SaveToStream(Image imageToSave, ISurface surface, Stream stream, SurfaceOutputSettings outputSettings) {
ImageFormat imageFormat = null; ImageFormat imageFormat = null;
bool useMemoryStream = false; bool useMemoryStream = false;
MemoryStream memoryStream = null; MemoryStream memoryStream = null;
if (outputSettings.Format == OutputFormat.greenshot && surface == null) {
throw new ArgumentException("Surface needs to be se when using OutputFormat.Greenshot");
}
try { try {
switch (outputSettings.Format) { switch (outputSettings.Format) {

View file

@ -254,18 +254,39 @@ namespace GreenshotPlugin.Core {
} }
/// <summary> /// <summary>
/// Write Multipart Form Data directly to the HttpWebRequest response stream /// Write Multipart Form Data directly to the HttpWebRequest
/// </summary> /// </summary>
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param> /// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
/// <param name="postParameters">Parameters to include in the multipart form data</param> /// <param name="postParameters">Parameters to include in the multipart form data</param>
public static void WriteMultipartFormData(HttpWebRequest webRequest, IDictionary<string, object> postParameters) { public static void WriteMultipartFormData(HttpWebRequest webRequest, IDictionary<string, object> postParameters) {
string boundary = String.Format("----------{0:N}", Guid.NewGuid()); string boundary = String.Format("----------{0:N}", Guid.NewGuid());
webRequest.ContentType = "multipart/form-data; boundary=" + boundary; webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
bool needsCLRF = false;
using (Stream formDataStream = webRequest.GetRequestStream()) { using (Stream formDataStream = webRequest.GetRequestStream()) {
WriteMultipartFormData(formDataStream, boundary, postParameters);
}
}
/// <summary>
/// Write Multipart Form Data to the HttpListenerResponse
/// </summary>
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
/// <param name="postParameters">Parameters to include in the multipart form data</param>
public static void WriteMultipartFormData(HttpListenerResponse response, IDictionary<string, object> postParameters) {
string boundary = String.Format("----------{0:N}", Guid.NewGuid());
response.ContentType = "multipart/form-data; boundary=" + boundary;
WriteMultipartFormData(response.OutputStream, boundary, postParameters);
}
/// <summary>
/// Write Multipart Form Data to a Stream, content-type should be set before this!
/// </summary>
/// <param name="formDataStream">Stream to write the multipart form data to</param>
/// <param name="boundary">String boundary for the multipart/form-data</param>
/// <param name="postParameters">Parameters to include in the multipart form data</param>
public static void WriteMultipartFormData(Stream formDataStream, string boundary, IDictionary<string, object> postParameters) {
bool needsCLRF = false;
foreach (var param in postParameters) { foreach (var param in postParameters) {
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added. // Add a CRLF to allow multiple parameters to be added.
// Skip it on the first parameter, add it to subsequent parameters. // Skip it on the first parameter, add it to subsequent parameters.
if (needsCLRF) { if (needsCLRF) {
formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n")); formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
@ -289,7 +310,6 @@ namespace GreenshotPlugin.Core {
string footer = "\r\n--" + boundary + "--\r\n"; string footer = "\r\n--" + boundary + "--\r\n";
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer)); formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
} }
}
/// <summary> /// <summary>
/// Process the web response. /// Process the web response.
@ -330,18 +350,18 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// A container to supply files to a Multi-part form data upload /// A container to supply files to a Multi-part form data upload
/// </summary> /// </summary>
public class FileContainer : IBinaryContainer { public class ByteContainer : IBinaryContainer {
private byte[] file; private byte[] file;
private string fileName; private string fileName;
private string contentType; private string contentType;
private int fileSize; private int fileSize;
public FileContainer(byte[] file) : this(file, null) { public ByteContainer(byte[] file) : this(file, null) {
} }
public FileContainer(byte[] file, string filename) : this(file, filename, null) { public ByteContainer(byte[] file, string filename) : this(file, filename, null) {
} }
public FileContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) { public ByteContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) {
} }
public FileContainer(byte[] file, string filename, string contenttype, int filesize) { public ByteContainer(byte[] file, string filename, string contenttype, int filesize) {
this.file = file; this.file = file;
this.fileName = filename; this.fileName = filename;
this.contentType = contenttype; this.contentType = contenttype;
@ -412,6 +432,82 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// A container to supply images to a Multi-part form data upload /// A container to supply images to a Multi-part form data upload
/// </summary> /// </summary>
public class BitmapContainer : IBinaryContainer {
private Bitmap bitmap;
private SurfaceOutputSettings outputSettings;
private string fileName;
public BitmapContainer(Bitmap bitmap, SurfaceOutputSettings outputSettings, string filename) {
this.bitmap = bitmap;
this.outputSettings = outputSettings;
this.fileName = filename;
}
/// <summary>
/// Create a Base64String from the image by saving it to a memory stream and converting it.
/// Should be avoided if possible, as this uses a lot of memory.
/// </summary>
/// <returns>string</returns>
public string ToBase64String(Base64FormattingOptions formattingOptions) {
using (MemoryStream stream = new MemoryStream()) {
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
return System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions);
}
}
/// <summary>
/// Create a byte[] from the image by saving it to a memory stream.
/// Should be avoided if possible, as this uses a lot of memory.
/// </summary>
/// <returns>byte[]</returns>
public byte[] ToByteArray() {
using (MemoryStream stream = new MemoryStream()) {
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
return stream.ToArray();
}
}
/// <summary>
/// Write Multipart Form Data directly to the HttpWebRequest response stream
/// </summary>
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
/// <param name="postParameters">Parameters to include in the multipart form data</param>
public void WriteFormDataToStream(string boundary, string key, Stream formDataStream) {
// Add just the first part of this param, since we will write the file data directly to the Stream
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
key,
fileName ?? key,
"image/" + outputSettings.Format.ToString());
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
ImageOutput.SaveToStream(bitmap, null, formDataStream, outputSettings);
}
/// <summary>
/// A plain "write data to stream"
/// </summary>
/// <param name="dataStream"></param>
public void WriteToStream(Stream dataStream) {
// Write the file data directly to the Stream, rather than serializing it to a string.
ImageOutput.SaveToStream(bitmap, null, dataStream, outputSettings);
}
/// <summary>
/// Upload the image to the webrequest
/// </summary>
/// <param name="webRequest"></param>
public void Upload(HttpWebRequest webRequest) {
webRequest.ContentType = "image/" + outputSettings.Format.ToString();
using (var requestStream = webRequest.GetRequestStream()) {
WriteToStream(requestStream);
}
}
}
/// <summary>
/// A container to supply surfaces to a Multi-part form data upload
/// </summary>
public class SurfaceContainer : IBinaryContainer { public class SurfaceContainer : IBinaryContainer {
private ISurface surface; private ISurface surface;
private SurfaceOutputSettings outputSettings; private SurfaceOutputSettings outputSettings;
@ -424,7 +520,7 @@ namespace GreenshotPlugin.Core {
} }
/// <summary> /// <summary>
/// Create a Base64String from the image by saving it to a memory stream and converting it. /// Create a Base64String from the Surface by saving it to a memory stream and converting it.
/// Should be avoided if possible, as this uses a lot of memory. /// Should be avoided if possible, as this uses a lot of memory.
/// </summary> /// </summary>
/// <returns>string</returns> /// <returns>string</returns>
@ -474,7 +570,7 @@ namespace GreenshotPlugin.Core {
} }
/// <summary> /// <summary>
/// Upload the image to the webrequest /// Upload the Surface as image to the webrequest
/// </summary> /// </summary>
/// <param name="webRequest"></param> /// <param name="webRequest"></param>
public void Upload(HttpWebRequest webRequest) { public void Upload(HttpWebRequest webRequest) {