Fixed bug where the image exported to the Clipboard, Jira, Imgur or Confluence is bigger as it should be. The reason is that "MemoryStream.getBuffer()" returns the byte buffer for the stream, which is usually bigger (capacity) than the actual data!!

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1702 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-03-14 11:53:03 +00:00
commit 40807d5d2b
5 changed files with 70 additions and 69 deletions

View file

@ -315,7 +315,7 @@ EndSelection:<<<<<<<4
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${width}", image.Width.ToString());
utf8EncodedHTMLString= utf8EncodedHTMLString.Replace("${height}", image.Height.ToString());
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${format}", "png");
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${data}", Convert.ToBase64String(pngStream.GetBuffer()));
utf8EncodedHTMLString = utf8EncodedHTMLString.Replace("${data}", Convert.ToBase64String(pngStream.GetBuffer(),0, (int)pngStream.Length));
StringBuilder sb=new StringBuilder();
sb.Append(utf8EncodedHTMLString);
sb.Replace("<<<<<<<1", (utf8EncodedHTMLString.IndexOf("<HTML>") + "<HTML>".Length).ToString("D8"));

View file

@ -141,40 +141,42 @@ namespace GreenshotConfluencePlugin {
}
private bool upload(Image image, Page page, string filename, bool openPage) {
byte[] buffer;
using (MemoryStream stream = new MemoryStream()) {
ConfluencePlugin.Host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
byte [] buffer = stream.GetBuffer();
try {
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, buffer);
LOG.Debug("Uploaded to Confluence.");
if (config.CopyWikiMarkupForImageToClipboard) {
int retryCount = 2;
while (retryCount >= 0) {
try {
Clipboard.SetText("!" + filename + "!");
break;
} catch (Exception ee) {
if (retryCount == 0) {
LOG.Error(ee);
} else {
Thread.Sleep(100);
}
} finally {
--retryCount;
// COPY buffer to array
buffer = stream.ToArray();
}
try {
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, buffer);
LOG.Debug("Uploaded to Confluence.");
if (config.CopyWikiMarkupForImageToClipboard) {
int retryCount = 2;
while (retryCount >= 0) {
try {
Clipboard.SetText("!" + filename + "!");
break;
} catch (Exception ee) {
if (retryCount == 0) {
LOG.Error(ee);
} else {
Thread.Sleep(100);
}
} finally {
--retryCount;
}
}
if (openPage) {
try {
Process.Start(page.Url);
} catch {}
} else {
System.Windows.MessageBox.Show(lang.GetString(LangKey.upload_success));
}
return true;
} catch(Exception e) {
System.Windows.MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
}
if (openPage) {
try {
Process.Start(page.Url);
} catch {}
} else {
System.Windows.MessageBox.Show(lang.GetString(LangKey.upload_success));
}
return true;
} catch(Exception e) {
System.Windows.MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
}
return false;
}

View file

@ -141,11 +141,9 @@ namespace GreenshotImgurPlugin {
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Attributes.Name, lang.GetString(LangKey.communication_wait));
host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
byte[] buffer = stream.GetBuffer();
try {
string filename = Path.GetFileName(host.GetFilename(config.UploadFormat, captureDetails));
ImgurInfo imgurInfo = ImgurUtils.UploadToImgur(buffer, captureDetails.DateTime.ToString(), filename);
ImgurInfo imgurInfo = ImgurUtils.UploadToImgur(stream.GetBuffer(), (int)stream.Length, captureDetails.DateTime.ToString(), 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);
@ -181,10 +179,9 @@ namespace GreenshotImgurPlugin {
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Attributes.Name, lang.GetString(LangKey.communication_wait));
imageEditor.SaveToStream(stream, config.UploadFormat, config.UploadJpegQuality);
byte [] buffer = stream.GetBuffer();
try {
string filename = Path.GetFileName(host.GetFilename(config.UploadFormat, imageEditor.CaptureDetails));
ImgurInfo imgurInfo = ImgurUtils.UploadToImgur(buffer, imageEditor.CaptureDetails.Title, filename);
ImgurInfo imgurInfo = ImgurUtils.UploadToImgur(stream.GetBuffer(), (int)stream.Length, imageEditor.CaptureDetails.Title, filename);
imageEditor.Surface.Modified = false;
LOG.InfoFormat("Storing imgur upload for hash {0} and delete hash {1}", imgurInfo.Hash, imgurInfo.DeleteHash);
config.ImgurUploadHistory.Add(imgurInfo.Hash, imgurInfo.DeleteHash);

View file

@ -85,11 +85,11 @@ namespace GreenshotImgurPlugin {
/// </summary>
/// <param name="imageData">byte[] with image data</param>
/// <returns>ImgurResponse</returns>
public static ImgurInfo UploadToImgur(byte[] imageData, string title, string filename) {
public static ImgurInfo UploadToImgur(byte[] imageData, int dataLength, string title, string filename) {
StringBuilder uploadRequest = new StringBuilder();
// Add image
uploadRequest.Append("image=");
uploadRequest.Append(HttpUtility.UrlEncode(System.Convert.ToBase64String(imageData)));
uploadRequest.Append(HttpUtility.UrlEncode(System.Convert.ToBase64String(imageData, 0, dataLength)));
// add key
uploadRequest.Append("&");
uploadRequest.Append("key=");

View file

@ -101,25 +101,27 @@ namespace GreenshotJiraPlugin {
public override bool ExportCapture(ISurface surface, ICaptureDetails captureDetails) {
string filename = Path.GetFileName(jiraPlugin.Host.GetFilename(config.UploadFormat, captureDetails));
byte[] buffer;
if (jira != null) {
using (MemoryStream stream = new MemoryStream()) {
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Description, lang.GetString(LangKey.communication_wait));
try {
using (Image image = surface.GetImageForExport()) {
jiraPlugin.Host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
}
byte [] buffer = stream.GetBuffer();
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, buffer);
LOG.Debug("Uploaded to Jira.");
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_success));
surface.SendMessageEvent(this, SurfaceMessageTyp.Info, jiraPlugin.Host.CoreLanguage.GetFormattedString("exported_to", Description));
surface.Modified = false;
return true;
} catch(Exception e) {
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
using (Image image = surface.GetImageForExport()) {
jiraPlugin.Host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
}
// COPY stream to buffer
buffer = stream.ToArray();
}
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Description, lang.GetString(LangKey.communication_wait));
try {
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, buffer);
LOG.Debug("Uploaded to Jira.");
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_success));
surface.SendMessageEvent(this, SurfaceMessageTyp.Info, jiraPlugin.Host.CoreLanguage.GetFormattedString("exported_to", Description));
surface.Modified = false;
return true;
} catch (Exception e) {
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
}
} else {
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
@ -129,24 +131,24 @@ namespace GreenshotJiraPlugin {
DialogResult result = jiraForm.ShowDialog();
if (result == DialogResult.OK) {
using (MemoryStream stream = new MemoryStream()) {
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Description, lang.GetString(LangKey.communication_wait));
try {
using (Image image = surface.GetImageForExport()) {
jiraPlugin.Host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
}
byte [] buffer = stream.GetBuffer();
jiraForm.upload(buffer);
LOG.Debug("Uploaded to Jira.");
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_success));
surface.SendMessageEvent(this, SurfaceMessageTyp.Info, "Exported to Jira " + jiraForm.getJiraIssue().Key);
surface.Modified = false;
return true;
} catch(Exception e) {
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
using (Image image = surface.GetImageForExport()) {
jiraPlugin.Host.SaveToStream(image, stream, config.UploadFormat, config.UploadJpegQuality);
}
// COPY stream to buffer
buffer = stream.ToArray();
}
BackgroundForm backgroundForm = BackgroundForm.ShowAndWait(Description, lang.GetString(LangKey.communication_wait));
try {
jiraForm.upload(buffer);
LOG.Debug("Uploaded to Jira.");
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_success));
surface.SendMessageEvent(this, SurfaceMessageTyp.Info, "Exported to Jira " + jiraForm.getJiraIssue().Key);
surface.Modified = false;
return true;
} catch(Exception e) {
backgroundForm.CloseDialog();
MessageBox.Show(lang.GetString(LangKey.upload_failure) + " " + e.Message);
}
}
}