Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Monday, January 31, 2011

PictureBox with transparent overlay picture (C#)

Working yesterday on my remote control application that takes screenshots from a machine running XBMC I wanted to provide a better experience for the user by providing a “loading, please wait” transparent overlay image on top of the old screenshot image until new one comes in. Swimming through a lot of either complicated (Graphics.DrawImage in onPaint) or useless suggestions, I found one close to what I had in mind but didn’t give much attention to initially - using two PictureBox controls on top of each other, one for the main image and one for the overlay.

The trick is to change the background colour for the overlay picturebox to transparent and also change its parent. On top of that, if the main PictureBox is not in the top left corner and you want the overlay image to be centered, you also need to change the location of the overlay PictureBox relative to the new parent, otherwise it keeps the originally design margins.

In my case:
  • pictureBoxMain – displays the main image, image size mode Zoom;
  • pictureBoxOverlay – displays the “wait” transparent image, same size and anchoring, image size mode Center;
private void Form1_Load(object sender, EventArgs e)
{
  // Transparent background...  
  pictureBoxOverlay.BackColor = Color.Transparent;

  // Change parent for overlay PictureBox...
  pictureBoxOverlay.Parent    = pictureBoxMain;

  // Change overlay PictureBox position in new parent...
  pictureBoxOverlay.Location  = new Point(0, 0);
}


Sunday, June 13, 2010

ImageShack.us image upload (C# code)

Spent a couple hours yesterday looking for some code to upload images to ImageShack.us (I need it for a XBMC for Xbox remote control app I’m writting) and thought I might share the code.

The image I have is already available in a PictureBox and this only concentrates on the upload itself, won’t go into parsing the XML reply, that should be fairly easy to implement it yourself…

More information about the ImageShack.us Upload API and how to apply for a key here:
http://code.google.com/p/imageshackapi/wiki/ImageshackAPI

Working example here:
https://code.google.com/p/dandar3-apps/source/browse/#svn/trunk/ImageShack Upload

/**
 * ImageShack Unified upload API
 * http://code.google.com/p/imageshackapi/wiki/ImageshackAPI
 */
private void buttonUpload_Click(object sender, EventArgs e)
{
  //
  Cursor.Current = Cursors.WaitCursor;

  //
  // Init
  //
  String      filename    = "fake_name";
  String      mimeType    = "image/jpeg";
  ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;

  //
  // Prepare POST contents...
  //
  MemoryStream memoryStream = new MemoryStream();

  // Parts boundary
  String boundary = "-------" + DateTime.Now.Ticks.ToString("x");
  String crlf     = "\r\n";
  
  // Image file...
  write(memoryStream, "--" + boundary + crlf);
  write(memoryStream, String.Format("Content-Disposition: form-data; name=\"fileupload\"; filename=\"{0}\"", filename) + crlf);
  write(memoryStream, String.Format("Content-Type: {0}", mimeType) + crlf);
  write(memoryStream, crlf);
  
  pictureBox.Image.Save(memoryStream, imageFormat);
  write(memoryStream, crlf);
  
  // Other parameters (Key, Username, Password etc)
  NameValueCollection parameters = new NameValueCollection();
  parameters.Add("key",        "your_key");
  parameters.Add("a_username", "your_username");
  parameters.Add("a_password", "your_password");
  parameters.Add("tags",       "your_tags");

  foreach (String param_name in parameters.Keys)
  {
    write(memoryStream, "--" + boundary + crlf);
    write(memoryStream, String.Format("Content-Disposition: form-data; name=\"{0}\"", param_name) + crlf);
    write(memoryStream, crlf);
    write(memoryStream, parameters.Get(param_name));
    write(memoryStream, crlf);
  }

  // End...
  write(memoryStream, "--" + boundary + crlf);
  
  //
  // Upload (POST)...
  //
  try
  {
    HttpWebRequest webRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");
    webRequest.ContentType    = "multipart/form-data; boundary=" + boundary;
    webRequest.Method         = "POST";

    //
    // Request
    //
    webRequest.ContentLength = memoryStream.Length;
    using (Stream requestStream = webRequest.GetRequestStream())
    {
      memoryStream.Seek(0, SeekOrigin.Begin);
      memoryStream.WriteTo(requestStream);
    }

    //
    // Response
    //
    using (WebResponse  webResponse  = webRequest.GetResponse())
    using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
    {
      String imageshackReply = streamReader.ReadToEnd().Trim();
      MessageBox.Show(imageshackReply, "HTTP POST Response", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "HTTP POST Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }

  //
  // Clean-up...
  //
  memoryStream.Close();
}

/**
 * 
 */
private void write(MemoryStream p_memoryStream, String p_data)
{
  byte[] bytes = Encoding.ASCII.GetBytes(p_data);
  p_memoryStream.Write(bytes, 0, bytes.Length);
}