Refactoring which replaces the Image in the ImageOutput for an ISurface, this makes it possible to save the Surface to any possible file format and even make it possible to pass some additional information in the OutputSettings. (Except reducing the colors, there are no other settings yet)

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2376 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-12-11 14:36:20 +00:00
parent e11450f56f
commit 5541e2e1c7
27 changed files with 191 additions and 290 deletions

View file

@ -71,10 +71,8 @@ namespace Greenshot.Destinations {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
try {
using (Image image = surface.GetImageForExport()) {
ClipboardHelper.SetClipboardData(image);
ClipboardHelper.SetClipboardData(surface);
exportInformation.ExportMade = true;
}
} catch (Exception) {
// TODO: Change to general logic in ProcessExport
surface.SendMessageEvent(this, SurfaceMessageTyp.Error, Language.GetString(LangKey.editor_clipboardfailed));

View file

@ -55,13 +55,11 @@ namespace GreenshotBoxPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
string uploadUrl = plugin.Upload(captureDetails, image);
string uploadUrl = plugin.Upload(captureDetails, surface);
if (uploadUrl != null) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadUrl;
}
}
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -110,12 +110,12 @@ namespace GreenshotBoxPlugin {
/// <summary>
/// This will be called when the menu item in the Editor is clicked
/// </summary>
public string Upload(ICaptureDetails captureDetails, Image image) {
public string Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, false);
try {
string url = null;
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
ImageContainer imageToUpload = new ImageContainer(image, outputSettings, filename);
SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
new PleaseWaitForm().ShowAndWait(BoxPlugin.Attributes.Name, Language.GetString("box", LangKey.communication_wait),
delegate() {

View file

@ -92,7 +92,7 @@ namespace GreenshotBoxPlugin {
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <returns>url to uploaded image</returns>
public static string UploadToBox(ImageContainer image, string title, string filename) {
public static string UploadToBox(SurfaceContainer image, string title, string filename) {
string folderId = "0";
if (string.IsNullOrEmpty(config.BoxToken)) {
if (!Authorize()) {

View file

@ -136,9 +136,8 @@ namespace GreenshotConfluencePlugin {
}
}
if (selectedPage != null) {
using (Image image = surface.GetImageForExport()) {
string errorMessage;
bool uploaded = upload(image, selectedPage, filename, out errorMessage);
bool uploaded = upload(surface, selectedPage, filename, out errorMessage);
if (uploaded) {
if (openPage) {
try {
@ -151,18 +150,17 @@ namespace GreenshotConfluencePlugin {
exportInformation.ErrorMessage = errorMessage;
}
}
}
ProcessExport(exportInformation, surface);
return exportInformation;
}
private bool upload(Image image, Page page, string filename, out string errorMessage) {
private bool upload(ISurface surfaceToUpload, Page page, string filename, out string errorMessage) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
errorMessage = null;
try {
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("confluence", LangKey.communication_wait),
delegate() {
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, new ImageContainer(image, outputSettings, filename));
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
}
);
LOG.Debug("Uploaded to Confluence.");

View file

@ -54,9 +54,8 @@ namespace GreenshotDropboxPlugin {
public override ExportInformation ExportCapture(bool manually, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
string uploadURL = null;
bool uploaded = plugin.Upload(captureDetails, image, out uploadURL);
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL);
if (uploaded) {
exportInformation.Uri = uploadURL;
exportInformation.ExportMade = true;
@ -64,7 +63,6 @@ namespace GreenshotDropboxPlugin {
ClipboardHelper.SetClipboardData(uploadURL);
}
}
}
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -111,7 +111,7 @@ namespace GreenshotDropboxPlugin {
/// <summary>
/// This will be called when the menu item in the Editor is clicked
/// </summary>
public bool Upload(ICaptureDetails captureDetails, Image image, out string uploadUrl) {
public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadUrl) {
uploadUrl = null;
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, false);
try {
@ -119,7 +119,7 @@ namespace GreenshotDropboxPlugin {
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("dropbox", LangKey.communication_wait),
delegate() {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
dropboxUrl = DropboxUtils.UploadToDropbox(image, outputSettings, filename);
dropboxUrl = DropboxUtils.UploadToDropbox(surfaceToUpload, outputSettings, filename);
}
);
if (dropboxUrl == null) {

View file

@ -36,7 +36,7 @@ namespace GreenshotDropboxPlugin {
private DropboxUtils() {
}
public static string UploadToDropbox(Image image, OutputSettings outputSettings, string filename) {
public static string UploadToDropbox(ISurface surfaceToUpload, OutputSettings outputSettings, string filename) {
OAuthSession oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET);
oAuth.BrowserSize = new Size(1080, 650);
oAuth.CheckVerifier = false;
@ -48,7 +48,7 @@ namespace GreenshotDropboxPlugin {
oAuth.TokenSecret = config.DropboxTokenSecret;
try {
ImageContainer imageToUpload = new ImageContainer(image, outputSettings, filename);
SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
string uploadResponse = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://api-content.dropbox.com/1/files_put/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, imageToUpload);
LOG.DebugFormat("Upload response: {0}", uploadResponse);
} catch (Exception ex) {

View file

@ -54,9 +54,7 @@ namespace GreenshotFlickrPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
plugin.Upload(captureDetails, image, exportInformation);
}
plugin.Upload(captureDetails, surface, exportInformation);
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -110,14 +110,14 @@ namespace GreenshotFlickrPlugin
config.ShowConfigDialog();
}
public void Upload(ICaptureDetails captureDetails, Image image, ExportInformation exportInformation) {
public void Upload(ICaptureDetails captureDetails, ISurface surface, ExportInformation exportInformation) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, false);
try {
string flickrUrl = null;
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("flickr", LangKey.communication_wait),
delegate() {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
flickrUrl = FlickrUtils.UploadToFlickr(image, outputSettings, captureDetails.Title, filename);
flickrUrl = FlickrUtils.UploadToFlickr(surface, outputSettings, captureDetails.Title, filename);
}
);

View file

@ -44,7 +44,7 @@ namespace GreenshotFlickrPlugin {
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <returns>url to image</returns>
public static string UploadToFlickr(Image image, OutputSettings outputSettings, string title, string filename) {
public static string UploadToFlickr(ISurface surfaceToUpload, OutputSettings outputSettings, string title, string filename) {
OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret);
oAuth.BrowserSize = new Size(520, 800);
oAuth.CheckVerifier = false;
@ -76,7 +76,7 @@ namespace GreenshotFlickrPlugin {
signedParameters.Add("safety_level", string.Format("{0}", (int)config.SafetyLevel));
signedParameters.Add("hidden", config.HiddenFromSearch?"1":"2");
IDictionary<string, object> otherParameters = new Dictionary<string, object>();
otherParameters.Add("photo", new ImageContainer(image, outputSettings, filename));
otherParameters.Add("photo", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.flickr.com/services/upload/", signedParameters, otherParameters, null);
string photoId = GetPhotoId(response);

View file

@ -58,11 +58,9 @@ namespace GreenshotImgurPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
string uploadURL = null;
exportInformation.ExportMade = plugin.Upload(captureDetails, image, out uploadURL);
exportInformation.ExportMade = plugin.Upload(captureDetails, surface, out uploadURL);
exportInformation.Uri = uploadURL;
}
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -151,7 +151,7 @@ namespace GreenshotImgurPlugin {
/// <param name="image"></param>
/// <param name="uploadURL">out string for the url</param>
/// <returns>true if the upload succeeded</returns>
public bool Upload(ICaptureDetails captureDetails, Image image, out string uploadURL) {
public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadURL) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
try {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
@ -160,7 +160,7 @@ namespace GreenshotImgurPlugin {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("imgur", LangKey.communication_wait),
delegate() {
imgurInfo = ImgurUtils.UploadToImgur(image, outputSettings, captureDetails.Title, filename);
imgurInfo = ImgurUtils.UploadToImgur(surfaceToUpload, outputSettings, captureDetails.Title, filename);
LOG.InfoFormat("Storing imgur upload for hash {0} and delete hash {1}", imgurInfo.Hash, imgurInfo.DeleteHash);
config.ImgurUploadHistory.Add(imgurInfo.Hash, imgurInfo.DeleteHash);
config.runtimeImgurHistory.Add(imgurInfo.Hash, imgurInfo);
@ -168,7 +168,10 @@ namespace GreenshotImgurPlugin {
}
);
imgurInfo.Image = ImageHelper.CreateThumbnail(image, 90, 90);
// TODO: Optimize a second call for export
using (Image tmpImage = surfaceToUpload.GetImageForExport()) {
imgurInfo.Image = ImageHelper.CreateThumbnail(tmpImage, 90, 90);
}
IniConfig.Save();
uploadURL = null;
try {

View file

@ -94,12 +94,12 @@ namespace GreenshotImgurPlugin {
/// Do the actual upload to Imgur
/// For more details on the available parameters, see: http://api.imgur.com/resources_anon
/// </summary>
/// <param name="image">Image to upload</param>
/// <param name="surfaceToUpload">ISurface to upload</param>
/// <param name="outputSettings">OutputSettings for the image file format</param>
/// <param name="title">Title</param>
/// <param name="filename">Filename</param>
/// <returns>ImgurInfo with details</returns>
public static ImgurInfo UploadToImgur(Image image, OutputSettings outputSettings, string title, string filename) {
public static ImgurInfo UploadToImgur(ISurface surfaceToUpload, OutputSettings outputSettings, string title, string filename) {
IDictionary<string, object> uploadParameters = new Dictionary<string, object>();
IDictionary<string, object> otherParameters = new Dictionary<string, object>();
// add title
@ -120,7 +120,7 @@ namespace GreenshotImgurPlugin {
webRequest.ServicePoint.Expect100Continue = false;
try {
using (var requestStream = webRequest.GetRequestStream()) {
ImageOutput.SaveToStream(image, requestStream, outputSettings);
ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
}
using (WebResponse response = webRequest.GetResponse()) {
@ -156,7 +156,7 @@ namespace GreenshotImgurPlugin {
IniConfig.Save();
}
try {
otherParameters.Add("image", new ImageContainer(image, outputSettings, filename));
otherParameters.Add("image", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.imgur.com/2/account/images.xml", uploadParameters, otherParameters, null);
} catch (Exception ex) {
LOG.Error("Upload to imgur gave an exeption: ", ex);

View file

@ -99,26 +99,25 @@ namespace GreenshotJiraPlugin {
}
}
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surfaceToUpload, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
if (jira != null) {
using (Image image = surface.GetImageForExport()) {
try {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
delegate() {
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, new ImageContainer(image, outputSettings, filename));
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
}
);
LOG.Debug("Uploaded to Jira.");
exportInformation.ExportMade = true;
exportInformation.Uri = surface.UploadURL;
// TODO: This can't work:
exportInformation.Uri = surfaceToUpload.UploadURL;
} catch (Exception e) {
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
}
}
} else {
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
if (jiraPlugin.JiraConnector.isLoggedIn) {
@ -126,24 +125,23 @@ namespace GreenshotJiraPlugin {
DialogResult result = jiraForm.ShowDialog();
if (result == DialogResult.OK) {
try {
using (Image image = surface.GetImageForExport()) {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
delegate() {
jiraForm.upload(new ImageContainer(image, outputSettings, filename));
jiraForm.upload(new SurfaceContainer(surfaceToUpload, outputSettings, filename));
}
);
}
LOG.Debug("Uploaded to Jira.");
exportInformation.ExportMade = true;
exportInformation.Uri = surface.UploadURL;
// TODO: This can't work:
exportInformation.Uri = surfaceToUpload.UploadURL;
} catch(Exception e) {
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
}
}
}
}
ProcessExport(exportInformation, surface);
ProcessExport(exportInformation, surfaceToUpload);
return exportInformation;
}
}

View file

@ -138,23 +138,9 @@ namespace GreenshotOCR {
public string DoOCR(ISurface surface) {
string filePath = null;
OutputSettings outputSettings = new OutputSettings(OutputFormat.bmp, 0, true);
// Use surface background image, this prevents having a mouse cursor in the way.
Image capturedImage = surface.Image;
if (capturedImage.Width < MIN_WIDTH || capturedImage.Height < MIN_HEIGHT) {
LOG.Debug("Captured image is not big enough for OCR, growing image...");
int newWidth = Math.Max(capturedImage.Width, MIN_WIDTH);
int newHeight = Math.Max(capturedImage.Height, MIN_HEIGHT);
using (Image tmpImage = new Bitmap(newWidth, newHeight, capturedImage.PixelFormat)) {
using (Graphics graphics = Graphics.FromImage(tmpImage)) {
graphics.Clear(Color.White);
graphics.DrawImage(capturedImage, Point.Empty);
}
filePath = ImageOutput.SaveToTmpFile(tmpImage, outputSettings, null);
}
} else {
filePath = ImageOutput.SaveToTmpFile(capturedImage, outputSettings, null);
}
// TODO: Add some filter & output settings so the output image is easier to process for the MODI-OCR code
// Also we need to check the size, resize if needed to 130x130 this is the minimum
filePath = ImageOutput.SaveToTmpFile(surface, outputSettings, null);
LOG.Debug("Saved tmp file to: " + filePath);

View file

@ -109,17 +109,15 @@ namespace GreenshotOfficePlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
if (page != null) {
try {
OneNoteExporter.ExportToPage((Bitmap)image, page);
OneNoteExporter.ExportToPage(surface, page);
exportInformation.ExportMade = true;
} catch (Exception ex) {
exportInformation.ErrorMessage = ex.Message;
LOG.Error(ex);
}
}
}
return exportInformation;
}
}

View file

@ -43,12 +43,12 @@ namespace Greenshot.Interop.Office {
private const string ONENOTE_NAMESPACE_2007 = "http://schemas.microsoft.com/office/onenote/2007/onenote";
private const string ONENOTE_NAMESPACE_2010 = "http://schemas.microsoft.com/office/onenote/2010/onenote";
public static void ExportToPage(Bitmap imageToExport, OneNotePage page) {
public static void ExportToPage(ISurface surfaceToUpload, OneNotePage page) {
using (MemoryStream pngStream = new MemoryStream()) {
OutputSettings pngOutputSettings = new OutputSettings(OutputFormat.png, 100, false);
ImageOutput.SaveToStream(imageToExport, pngStream, pngOutputSettings);
ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
string base64String = Convert.ToBase64String(pngStream.GetBuffer());
string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, imageToExport.Width, imageToExport.Height);
string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName });
using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance<IOneNoteApplication>()) {
LOG.InfoFormat("Sending XML: {0}", pageChangesXml);

View file

@ -58,14 +58,12 @@ namespace GreenshotPhotobucketPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
string uploadURL = null;
bool uploaded = plugin.Upload(captureDetails, image, out uploadURL);
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL);
if (uploaded) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadURL;
}
}
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -111,10 +111,10 @@ namespace GreenshotPhotobucketPlugin {
/// Upload the capture to Photobucket
/// </summary>
/// <param name="captureDetails"></param>
/// <param name="image"></param>
/// <param name="surfaceToUpload">ISurface</param>
/// <param name="uploadURL">out string for the url</param>
/// <returns>true if the upload succeeded</returns>
public bool Upload(ICaptureDetails captureDetails, Image image, out string uploadURL) {
public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadURL) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
try {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
@ -123,7 +123,7 @@ namespace GreenshotPhotobucketPlugin {
// Run upload in the background
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("photobucket", LangKey.communication_wait),
delegate() {
photobucketInfo = PhotobucketUtils.UploadToPhotobucket(image, outputSettings, captureDetails.Title, filename);
photobucketInfo = PhotobucketUtils.UploadToPhotobucket(surfaceToUpload, outputSettings, captureDetails.Title, filename);
}
);
// This causes an exeption if the upload failed :)

View file

@ -40,9 +40,8 @@ namespace GreenshotPhotobucketPlugin {
/// Do the actual upload to Photobucket
/// For more details on the available parameters, see: http://api.Photobucket.com/resources_anon
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <returns>PhotobucketResponse</returns>
public static PhotobucketInfo UploadToPhotobucket(Image image, OutputSettings outputSettings, string title, string filename) {
public static PhotobucketInfo UploadToPhotobucket(ISurface surfaceToUpload, OutputSettings outputSettings, string title, string filename) {
string responseString;
OAuthSession oAuth = new OAuthSession(PhotobucketCredentials.ConsumerKey, PhotobucketCredentials.ConsumerSecret);
@ -94,7 +93,7 @@ namespace GreenshotPhotobucketPlugin {
}
IDictionary<string, object> unsignedParameters = new Dictionary<string, object>();
// Add image
unsignedParameters.Add("uploadfile", new ImageContainer(image, outputSettings, filename));
unsignedParameters.Add("uploadfile", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
try {
string apiUrl = "http://api.photobucket.com/album/!/upload";
responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, apiUrl, apiUrl.Replace("api.photobucket.com", config.SubDomain), signedParameters, unsignedParameters, null);

View file

@ -54,14 +54,12 @@ namespace GreenshotPicasaPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
using (Image image = surface.GetImageForExport()) {
string uploadURL = null;
bool uploaded = plugin.Upload(captureDetails, image, out uploadURL);
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL);
if (uploaded) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadURL;
}
}
ProcessExport(exportInformation, surface);
return exportInformation;
}

View file

@ -109,7 +109,7 @@ namespace GreenshotPicasaPlugin {
Configure();
}
public bool Upload(ICaptureDetails captureDetails, Image image, out string uploadUrl) {
public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadUrl) {
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality);
try {
string url = null;
@ -117,7 +117,7 @@ namespace GreenshotPicasaPlugin {
delegate() {
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
string contentType = "image/" + config.UploadFormat.ToString();
url = PicasaUtils.UploadToPicasa(image, outputSettings, captureDetails.Title, filename);
url = PicasaUtils.UploadToPicasa(surfaceToUpload, outputSettings, captureDetails.Title, filename);
}
);
uploadUrl = url;

View file

@ -41,7 +41,7 @@ namespace GreenshotPicasaPlugin {
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <returns>PicasaResponse</returns>
public static string UploadToPicasa(Image imageToUpload, OutputSettings outputSettings, string title, string filename) {
public static string UploadToPicasa(ISurface surfaceToUpload, OutputSettings outputSettings, string title, string filename) {
OAuthSession oAuth = new OAuthSession(PicasaCredentials.ConsumerKey, PicasaCredentials.ConsumerSecret);
oAuth.BrowserSize = new Size(1020, 590);
oAuth.AccessTokenUrl = "https://www.google.com/accounts/OAuthGetAccessToken";
@ -67,7 +67,7 @@ namespace GreenshotPicasaPlugin {
try {
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("slug", OAuthSession.UrlEncode3986(filename));
string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", headers, null, null, new ImageContainer(imageToUpload, outputSettings, filename));
string response = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://picasaweb.google.com/data/feed/api/user/default/albumid/default", headers, null, null, new SurfaceContainer(surfaceToUpload, outputSettings, filename));
return ParseResponse(response);
} catch (Exception ex) {
LOG.Error("Upload error: ", ex);

View file

@ -335,10 +335,10 @@ EndSelection:<<<<<<<4
SetDataObject(ido);
}
private static string getHTMLString(Image image, string filename) {
private static string getHTMLString(ISurface surface, string filename) {
string utf8EncodedHTMLString = Encoding.GetEncoding(0).GetString(Encoding.UTF8.GetBytes(HTML_CLIPBOARD_STRING));
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${width}", image.Width.ToString());
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${height}", image.Height.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${width}", surface.Image.Width.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${height}", surface.Image.Height.ToString());
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${file}", filename);
StringBuilder sb=new StringBuilder();
sb.Append(utf8EncodedHTMLString);
@ -349,10 +349,10 @@ EndSelection:<<<<<<<4
return sb.ToString();
}
private static string getHTMLDataURLString(Image image, MemoryStream pngStream) {
private static string getHTMLDataURLString(ISurface surface, MemoryStream pngStream) {
string utf8EncodedHTMLString = Encoding.GetEncoding(0).GetString(Encoding.UTF8.GetBytes(HTML_CLIPBOARD_BASE64_STRING));
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${width}", image.Width.ToString());
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${height}", image.Height.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${width}", surface.Image.Width.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${height}", surface.Image.Height.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${format}", "png");
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${data}", Convert.ToBase64String(pngStream.GetBuffer(),0, (int)pngStream.Length));
StringBuilder sb=new StringBuilder();
@ -374,7 +374,7 @@ EndSelection:<<<<<<<4
/// For this problem the user should not use the direct paste (=Dib), but select Bitmap
/// </summary>
private const int BITMAPFILEHEADER_LENGTH = 14;
public static void SetClipboardData(Image image) {
public static void SetClipboardData(ISurface surface) {
DataObject ido = new DataObject();
// This will work for Office and most other applications
@ -389,7 +389,7 @@ EndSelection:<<<<<<<4
pngStream = new MemoryStream();
// PNG works for Powerpoint
OutputSettings pngOutputSettings = new OutputSettings(OutputFormat.png, 100, false);
ImageOutput.SaveToStream(image, pngStream, pngOutputSettings);
ImageOutput.SaveToStream(surface, pngStream, pngOutputSettings);
pngStream.Seek(0, SeekOrigin.Begin);
}
@ -402,7 +402,7 @@ EndSelection:<<<<<<<4
bmpStream = new MemoryStream();
// Save image as BMP
OutputSettings bmpOutputSettings = new OutputSettings(OutputFormat.bmp, 100, false);
ImageOutput.SaveToStream(image, bmpStream, bmpOutputSettings);
ImageOutput.SaveToStream(surface, bmpStream, bmpOutputSettings);
imageStream = new MemoryStream();
// Copy the source, but skip the "BITMAPFILEHEADER" which has a size of 14
@ -414,11 +414,11 @@ EndSelection:<<<<<<<4
// Set the HTML
if (config.ClipboardFormats.Contains(ClipboardFormat.HTML)) {
string tmpFile = ImageOutput.SaveToTmpFile(image, new OutputSettings(OutputFormat.png), null);
string html = getHTMLString(image, tmpFile);
string tmpFile = ImageOutput.SaveToTmpFile(surface, new OutputSettings(OutputFormat.png), null);
string html = getHTMLString(surface, tmpFile);
ido.SetText(html, TextDataFormat.Html);
} else if (config.ClipboardFormats.Contains(ClipboardFormat.HTMLDATAURL)) {
string html = getHTMLDataURLString(image, pngStream);
string html = getHTMLDataURLString(surface, pngStream);
ido.SetText(html, TextDataFormat.Html);
}
} finally {

View file

@ -74,7 +74,7 @@ namespace GreenshotPlugin.Core {
/// To prevent problems with GDI version of before Windows 7:
/// the stream is checked if it's seekable and if needed a MemoryStream as "cache" is used.
/// </summary>
public static void SaveToStream(Image imageToSave, Stream stream, OutputSettings outputSettings) {
public static void SaveToStream(ISurface surface, Stream stream, OutputSettings outputSettings) {
ImageFormat imageFormat = null;
bool disposeImage = false;
bool useMemoryStream = false;
@ -93,6 +93,7 @@ namespace GreenshotPlugin.Core {
case OutputFormat.tiff:
imageFormat = ImageFormat.Tiff;
break;
case OutputFormat.greenshot:
case OutputFormat.png:
default:
// Problem with non-seekable streams most likely doesn't happen with Windows 7 (OS Version 6.1 and later)
@ -109,11 +110,28 @@ namespace GreenshotPlugin.Core {
break;
}
// Removing transparency if it's not supported
if (imageFormat != ImageFormat.Png) {
imageToSave = ImageHelper.Clone(imageToSave, PixelFormat.Format24bppRgb);
// check what image we want to save
Image imageToSave = null;
if (outputSettings.Format == OutputFormat.greenshot) {
// We save the image of the surface, this should not be disposed
imageToSave = surface.Image;
} else {
// We create the export image of the surface to save
imageToSave = surface.GetImageForExport();
disposeImage = true;
}
try {
// Removing transparency if it's not supported
if (imageFormat != ImageFormat.Png && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) {
Image nonAlphaImage = ImageHelper.Clone(imageToSave, PixelFormat.Format24bppRgb);
if (disposeImage) {
imageToSave.Dispose();
}
// Make sure the image is disposed!
disposeImage = true;
imageToSave = nonAlphaImage;
}
// check for color reduction, forced or automatically
if (conf.OutputFileAutoReduceColors || outputSettings.ReduceColors) {
@ -138,7 +156,6 @@ namespace GreenshotPlugin.Core {
LOG.Info("Skipping color reduction test, OutputFileAutoReduceColors is set to false.");
}
try {
// Create meta-data
PropertyItem softwareUsedPropertyItem = CreatePropertyItem(PROPERTY_TAG_SOFTWARE_USED, "Greenshot");
if (softwareUsedPropertyItem != null) {
@ -172,18 +189,27 @@ namespace GreenshotPlugin.Core {
if (!foundEncoder) {
throw new ApplicationException("No JPG encoder found, this should not happen.");
}
} else if (imageFormat != ImageFormat.Png && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) {
// No transparency in target format
using (Bitmap tmpBitmap = ImageHelper.Clone(imageToSave, PixelFormat.Format24bppRgb)) {
tmpBitmap.Save(targetStream, imageFormat);
}
} else {
imageToSave.Save(targetStream, imageFormat);
}
// If we used a memory stream, we need to stream the memory stream to the original stream.
if (useMemoryStream) {
memoryStream.WriteTo(stream);
}
// Output the surface elements, size and marker to the stream
if (outputSettings.Format == OutputFormat.greenshot) {
using (MemoryStream tmpStream = new MemoryStream()) {
long bytesWritten = surface.SaveElementsToStream(tmpStream);
using (BinaryWriter writer = new BinaryWriter(tmpStream)) {
writer.Write(bytesWritten);
Version v = Assembly.GetExecutingAssembly().GetName().Version;
byte[] marker = System.Text.Encoding.ASCII.GetBytes(String.Format("Greenshot{0:00}.{1:00}", v.Major, v.Minor));
writer.Write(marker);
tmpStream.WriteTo(stream);
}
}
}
} finally {
if (memoryStream != null) {
memoryStream.Dispose();
@ -195,36 +221,6 @@ namespace GreenshotPlugin.Core {
}
}
/// <summary>
/// Save a Greenshot surface
/// </summary>
/// <param name="surface">Surface to save</param>
/// <param name="fullPath">Path to file</param>
public static void SaveGreenshotSurface(ISurface 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);
Version v = Assembly.GetExecutingAssembly().GetName().Version;
string marker = String.Format("Greenshot{0:00}.{1:00}", v.Major, v.Minor);
using (StreamWriter streamWriter = new StreamWriter(stream)) {
streamWriter.Write(marker);
}
}
}
}
/// <summary>
/// Load a Greenshot surface
/// </summary>
@ -274,46 +270,6 @@ namespace GreenshotPlugin.Core {
return returnSurface;
}
/// <summary>
/// Saves image to specific path with specified quality
/// </summary>
public static void Save(Image image, string fullPath, bool allowOverwrite, OutputSettings outputSettings, bool copyPathToClipboard) {
fullPath = FilenameHelper.MakeFQFilenameSafe(fullPath);
string path = Path.GetDirectoryName(fullPath);
// check whether path exists - if not create it
DirectoryInfo di = new DirectoryInfo(path);
if (!di.Exists) {
Directory.CreateDirectory(di.FullName);
}
string extension = Path.GetExtension(fullPath);
if (extension != null && extension.StartsWith(".")) {
extension = extension.Substring(1);
}
OutputFormat format = OutputFormat.png;
try {
if (extension != null) {
format = (OutputFormat)Enum.Parse(typeof(OutputFormat), extension.ToLower());
}
} catch (ArgumentException ae) {
LOG.Warn("Couldn't parse extension: " + extension, ae);
}
if (!allowOverwrite && File.Exists(fullPath)) {
ArgumentException throwingException = new ArgumentException("File '" + fullPath + "' already exists.");
throwingException.Data.Add("fullPath", fullPath);
throw throwingException;
}
LOG.DebugFormat("Saving image to {0}", fullPath);
// Create the stream and call SaveToStream
using (FileStream stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) {
SaveToStream(image, stream, outputSettings);
}
if (copyPathToClipboard) {
ClipboardHelper.SetClipboardData(fullPath);
}
}
/// <summary>
/// Saves image to specific path with specified quality
/// </summary>
@ -332,16 +288,10 @@ namespace GreenshotPlugin.Core {
throwingException.Data.Add("fullPath", fullPath);
throw throwingException;
}
LOG.DebugFormat("Saving image to {0}", fullPath);
LOG.DebugFormat("Saving surface to {0}", fullPath);
// Create the stream and call SaveToStream
if (outputSettings.Format == OutputFormat.greenshot) {
SaveGreenshotSurface(surface, fullPath);
} else {
using (FileStream stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) {
using (Image image = surface.GetImageForExport()) {
SaveToStream(image, stream, outputSettings);
}
}
SaveToStream(surface, stream, outputSettings);
}
if (copyPathToClipboard) {
@ -367,23 +317,6 @@ namespace GreenshotPlugin.Core {
}
return format;
}
/// <summary>
/// saves img to fullpath
/// </summary>
/// <param name="img">the image to save</param>
/// <param name="fullPath">the absolute destination path including file name</param>
/// <param name="allowOverwrite">true if overwrite is allowed, false if not</param>
public static void Save(Image img, string fullPath, bool allowOverwrite) {
OutputFormat format = FormatForFilename(fullPath);
// Get output settings from the configuration
OutputSettings outputSettings = new OutputSettings(format);
if (conf.OutputFilePromptQuality) {
QualityDialog qualityDialog = new QualityDialog(outputSettings);
qualityDialog.ShowDialog();
}
Save(img, fullPath, allowOverwrite, outputSettings, conf.OutputFileCopyPathToClipboard);
}
#endregion
#region save-as
@ -460,7 +393,7 @@ namespace GreenshotPlugin.Core {
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static string SaveToTmpFile(Image image, OutputSettings outputSettings, string destinationPath) {
public static string SaveToTmpFile(ISurface surface, OutputSettings outputSettings, string destinationPath) {
string tmpFile = Path.GetRandomFileName() + "." + outputSettings.Format.ToString();
// Prevent problems with "other characters", which could cause problems
tmpFile = Regex.Replace(tmpFile, @"[^\d\w\.]", "");
@ -471,7 +404,7 @@ namespace GreenshotPlugin.Core {
LOG.Debug("Creating TMP File : " + tmpPath);
try {
ImageOutput.Save(image, tmpPath, true, outputSettings, false);
ImageOutput.Save(surface, tmpPath, true, outputSettings, false);
tmpFileCache.Add(tmpPath, tmpPath);
} catch (Exception) {
return null;

View file

@ -413,13 +413,13 @@ namespace GreenshotPlugin.Core {
/// <summary>
/// A container to supply images to a Multi-part form data upload
/// </summary>
public class ImageContainer : IBinaryContainer {
private Image image;
public class SurfaceContainer : IBinaryContainer {
private ISurface surface;
private OutputSettings outputSettings;
private string fileName;
public ImageContainer(Image image, OutputSettings outputSettings, string filename) {
this.image = image;
public SurfaceContainer(ISurface surface, OutputSettings outputSettings, string filename) {
this.surface = surface;
this.outputSettings = outputSettings;
this.fileName = filename;
}
@ -431,7 +431,7 @@ namespace GreenshotPlugin.Core {
/// <returns>string</returns>
public string ToBase64String(Base64FormattingOptions formattingOptions) {
using (MemoryStream stream = new MemoryStream()) {
ImageOutput.SaveToStream(image, stream, outputSettings);
ImageOutput.SaveToStream(surface, stream, outputSettings);
return System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions);
}
}
@ -443,7 +443,7 @@ namespace GreenshotPlugin.Core {
/// <returns>byte[]</returns>
public byte[] ToByteArray() {
using (MemoryStream stream = new MemoryStream()) {
ImageOutput.SaveToStream(image, stream, outputSettings);
ImageOutput.SaveToStream(surface, stream, outputSettings);
return stream.ToArray();
}
}
@ -462,7 +462,7 @@ namespace GreenshotPlugin.Core {
"image/" + outputSettings.Format.ToString());
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
ImageOutput.SaveToStream(image, formDataStream, outputSettings);
ImageOutput.SaveToStream(surface, formDataStream, outputSettings);
}
/// <summary>
@ -471,7 +471,7 @@ namespace GreenshotPlugin.Core {
/// <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(image, dataStream, outputSettings);
ImageOutput.SaveToStream(surface, dataStream, outputSettings);
}
/// <summary>