smart developer's blog

This is a C# resource library! Free how to's and best practices…

ASP.net Google API Authentication using ClientLogin for installed applications

with 3 comments

Here is an example of how you can authenticate in ASP.net C# in Google services/products. The example below is for Picasa. Each service using the Authorization service is assigned a name value; for example, the name associated with Google Picasa ‘lh2′, for Google Calendar is “cl”. This parameter is required when accessing services based on Google Data APIs. For specific service names, refer to the service documentation at: http://code.google.com/intl/en-GB/apis/accounts/docs/AuthForInstalledApps.html.

First, we will need to get the Authentication Token. You can do this for a specific Google Account using the code below:


public static string GetAuthenticationToken(string userName, string password)
{
	string response = RequestHelper.GetPostResult(
		"https://www.google.com/accounts/clientlogin",
		String.Format("accountType=HOSTED_OR_GOOGLE&Email={0}&Passwd={1}&service=lh2&souce=sourceappname", userName, password)); // lh2 = the service name for the Picasa Web Albums API is "lh2".
	if (response.Contains("Auth="))
	{
		foreach (string line in response.Split(Environment.NewLine.ToCharArray()))
		{
			if (line.StartsWith("Auth="))
			{
				response = line.Replace("Auth=", "");
				break;
			}
		}
	}
	else
	{
		response = "Error:" + response;
	}
	return response;
}

where the code of the RequestHelper.cs file is listed below:


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace PicasaNet.Helpers
{
	class RequestHelper
	{
		public static string GetPostResult(string url, string strPost)
		{
			string strResponse = "";
			try
			{
				UTF8Encoding objUTFEncode = new UTF8Encoding();
				byte[] arrRequest;
				Stream objStreamReq;
				StreamReader objStreamRes;
				HttpWebRequest objHttpRequest;
				HttpWebResponse objHttpResponse;
				Uri objUri = new Uri(url);

				objHttpRequest = (HttpWebRequest)HttpWebRequest.Create(objUri);
				objHttpRequest.KeepAlive = false;
				objHttpRequest.Method = "POST";

				objHttpRequest.ContentType = "application/x-www-form-urlencoded";
				arrRequest = objUTFEncode.GetBytes(strPost);
				objHttpRequest.ContentLength = arrRequest.Length;

				objStreamReq = objHttpRequest.GetRequestStream();
				objStreamReq.Write(arrRequest, 0, arrRequest.Length);
				objStreamReq.Close();

				//Get response
				objHttpResponse = (HttpWebResponse)objHttpRequest.GetResponse();
				objStreamRes = new StreamReader(objHttpResponse.GetResponseStream(), Encoding.ASCII);

				strResponse = objStreamRes.ReadToEnd();
				objStreamRes.Close();
			}
			catch(Exception ex)
			{
				strResponse = "Error:" + ex.Message;
			}
			return strResponse;
		}
	}
}

Now, basically if you want to read all the albums that a user is sharing in Picasa, all you have to do is to make a request to “http://picasaweb.google.com/data/feed/api/user/{username}” and parse the response xml file. However, this will only return public albums. If you want to list private albums as well, you will have to add the AuthenticationToken in the Header of your request. Here is the code:


public static XmlDocument GetRoot(string feedurl, string auth)
{
	XmlDocument doc = new XmlDocument();
	string result = "";
	HttpWebResponse response;
	WebRequest request = HttpWebRequest.Create(feedurl);

	if (!String.IsNullOrEmpty(auth))
		request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + auth);

	request.Method = "GET";
	response = (HttpWebResponse)request.GetResponse();

	if (response.StatusCode == HttpStatusCode.OK)
	{
		StreamReader sr = new StreamReader(response.GetResponseStream());
		result = sr.ReadToEnd();
		doc.LoadXml(result);
	}
	else
	{
		throw new Exception("Fault: Status code - " + response.StatusCode.ToString());
	}
	return doc;
}

By default, you get only private albums but if you append “?access=private” or “?access=all” to the request URL you will get all the albums. Further more, in order to get all the images in an album you will have to get this URL: “http://picasaweb.google.com/data/entry/api/user/{username}/albumid/{albumid}”.

If you want to know how you can parse the XML responsen, just check my other post at http://codehelp.smartdev.eu/2011/03/06/gereric-xml-object-read-any-xml-document-and-populate-a-asp-net-object-consume-xml-feeds/ or leave me a message. I will be happy to provide you with the full code for a Picasa.NET application.

Written by smartdev

May 31st, 2011 at 8:26 am

How to create watermark pictures in C#

with 3 comments

The code below will add a watermark to an existing image and it will save it to disk or it will display it (binary write it). It will add both an image (logo) in the corner of the original image and a text (your website name for example) centered over the image (you can see an example image below the code).

 public class ImageCheck
    {

public static void CreatePicture(string FromFile, bool BinaryWrite)
{
	//set a working directory
	string WorkingDirectory = HttpContext.Current.Server.MapPath("/images/WaterMark");

	//define a string of text to use as the Copyright message
	string Copyright = "Copyright © Your website";

	//create a image object containing the photograph to watermark
	Image imgPhoto = Image.FromFile(FromFile);
	int phWidth = imgPhoto.Width;
	int phHeight = imgPhoto.Height;

	//create a Bitmap the Size of the original photograph
	Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

	bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

	//load the Bitmap into a Graphics object
	Graphics grPhoto = Graphics.FromImage(bmPhoto);

	//create a image object containing the watermark
	Image imgWatermark = new Bitmap(HttpContext.Current.Server.MapPath("/images/watermark_photo.gif"));
	int wmWidth = imgWatermark.Width;
	int wmHeight = imgWatermark.Height;

	decimal percent = 0.25m; // percent of maximum watermark image
	if ((wmWidth > Convert.ToInt32(phWidth * percent)) || (wmHeight > Convert.ToInt32(phHeight * percent)))
	{
		int new_wmWidth = 0;
		int new_wmHeight = 0;

		if ((wmWidth / phWidth) > (phHeight / wmHeight))
		{
			new_wmWidth = Convert.ToInt32(phWidth * percent);
			new_wmHeight = Convert.ToInt32(new_wmWidth * (Convert.ToDecimal(wmHeight) / wmWidth));
		}
		else
		{
			new_wmHeight = Convert.ToInt32(phHeight * percent);
			new_wmWidth = Convert.ToInt32(new_wmHeight * (Convert.ToDecimal(wmWidth) / wmHeight));
		}
		wmHeight = new_wmHeight;
		wmWidth = new_wmWidth;

		imgWatermark = imgWatermark.GetThumbnailImage(wmWidth, wmHeight, new Image.GetThumbnailImageAbort(ImageCheck.ThumbnailCallback), IntPtr.Zero);
	}

	//------------------------------------------------------------
	//Step #1 - Insert Copyright message
	//------------------------------------------------------------

	//Set the rendering quality for this Graphics object
	grPhoto.SmoothingMode = SmoothingMode.HighQuality;

	//Draws the photo Image object at original size to the graphics object.
	grPhoto.DrawImage(
		imgPhoto,                               // Photo Image object
		new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
		0,                                      // x-coordinate of the portion of the source image to draw.
		0,                                      // y-coordinate of the portion of the source image to draw.
		phWidth,                                // Width of the portion of the source image to draw.
		phHeight,                               // Height of the portion of the source image to draw.
		GraphicsUnit.Pixel);                    // Units of measure 

	//-------------------------------------------------------
	//to maximize the size of the Copyright message we will
	//test multiple Font sizes to determine the largest posible
	//font we can use for the width of the Photograph
	//define an array of point sizes you would like to consider as possiblities
	//-------------------------------------------------------
	Font crFont = null;
	SizeF crSize = new SizeF();

	//Loop through the defined sizes checking the length of the Copyright string
	//If its length in pixles is less then the image width choose this Font size.

	percent = 0.8m; // percent of maximum watermark text
	for (int i = 30; i > 1; i--)
	{
		//set a Font object to Arial (i)pt, Bold
		crFont = new Font("arial", i, FontStyle.Bold);
		//Measure the Copyright string in this Font
		crSize = grPhoto.MeasureString(Copyright, crFont);

		//HttpContext.Current.Trace.Warn("Size tested: " + i + " -> Size of text: " + crSize.Width + "px");

		if ((ushort)crSize.Width < (ushort)(phWidth * percent))
		{
			HttpContext.Current.Trace.Warn("Size selected: " + i);
			break;
		}
	}

	//Since all photographs will have varying heights, determine a
	//position 5% from the bottom of the image
	int yPixlesFromBottom = (int)(phHeight *.05);

	//Now that we have a point size use the Copyrights string height
	//to determine a y-coordinate to draw the string of the photograph
	float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));

	//Determine its x-coordinate by calculating the center of the width of the image
	float xCenterOfImg = (phWidth / 2);
	float yCenterOfImg = (phHeight / 2);

	//Define the text layout by setting the text alignment to centered
	StringFormat StrFormat = new StringFormat();
	StrFormat.Alignment = StringAlignment.Center;

	//define a Brush which is semi trasparent black (Alpha set to 153)
	SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0, 0));

	//Draw the Copyright string
	grPhoto.DrawString(Copyright,                 //string of text
		crFont,                                   //font
		semiTransBrush2,                           //Brush
		new PointF(xCenterOfImg + 1, yCenterOfImg + 1),  //Position
		StrFormat);

	//define a Brush which is semi trasparent white (Alpha set to 153)
	SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));

	//Draw the Copyright string a second time to create a shadow effect
	//Make sure to move this text 1 pixel to the right and down 1 pixel
	grPhoto.DrawString(Copyright,                 //string of text
		crFont,                                   //font
		semiTransBrush,                           //Brush
		new PointF(xCenterOfImg, yCenterOfImg),   //Position
		StrFormat);                               //Text alignment

	//------------------------------------------------------------
	//Step #2 - Insert Watermark image
	//------------------------------------------------------------

	//Create a Bitmap based on the previously modified photograph Bitmap
	Bitmap bmWatermark = new Bitmap(bmPhoto);
	bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
	//Load this Bitmap into a new Graphic Object
	Graphics grWatermark = Graphics.FromImage(bmWatermark);

	//To achieve a transulcent watermark we will apply (2) color
	//manipulations by defineing a ImageAttributes object and
	//seting (2) of its properties.
	ImageAttributes imageAttributes = new ImageAttributes();

	//The first step in manipulating the watermark image is to replace
	//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
	//to do this we will use a Colormap and use this to define a RemapTable
	ColorMap colorMap = new ColorMap();

	//My watermark was defined with a background of 100% Green this will
	//be the color we search for and replace with transparency
	colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
	colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); 

	ColorMap[] remapTable = {colorMap};

	imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

	//The second color manipulation is used to change the opacity of the
	//watermark.  This is done by applying a 5x5 matrix that contains the
	//coordinates for the RGBA space.  By setting the 3rd row and 3rd column
	//to 0.3f we achive a level of opacity
	float[][] colorMatrixElements = {
										new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
										new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
										new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
										new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},
										new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
	ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

	imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
		ColorAdjustType.Bitmap);

	//For this example we will place the watermark in the upper right
	//hand corner of the photograph. offset down 10 pixels and to the
	//left 10 pixles

	int xPosOfWm = ((phWidth - wmWidth)-10);
	int yPosOfWm = 10;

	grWatermark.DrawImage(imgWatermark,
		new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight),  //Set the detination Position
		0,                  // x-coordinate of the portion of the source image to draw.
		0,                  // y-coordinate of the portion of the source image to draw.
		wmWidth,            // Watermark Width
		wmHeight,		    // Watermark Height
		GraphicsUnit.Pixel, // Unit of measurment
		imageAttributes);   //ImageAttributes Object

	//Replace the original photgraphs bitmap with the new Bitmap
	imgPhoto = bmWatermark;
	grPhoto.Dispose();
	grWatermark.Dispose();

	ImageCodecInfo ici = ImageCheck.GetEncoderInfo("image/jpeg");

	EncoderParameters ep = new EncoderParameters(1);
	ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)90);

	if (BinaryWrite)
	{
		MemoryStream imageStream = new MemoryStream();
		imgPhoto.Save(imageStream, ici, ep);

		byte[] imageContent = new Byte[imageStream.Length];
		imageStream.Position = 0;
		imageStream.Read(imageContent, 0, (int)imageStream.Length);
		HttpContext.Current.Response.Clear();

		HttpContext.Current.Response.ContentType = "image/jpeg";
		HttpContext.Current.Response.BinaryWrite(imageContent);
	}
	else
	{
		imgPhoto.Save(WorkingDirectory + "/" + System.IO.Path.GetFileName(FromFile), ici, ep);
		//imgPhoto.Save(WorkingDirectory + "/" + System.IO.Path.GetFileName(FromFile), ImageFormat.Jpeg);
	}

	imgPhoto.Dispose();
	imgWatermark.Dispose();
}

public static bool ThumbnailCallback()
        {
            return true;
        }

        public static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            } return null;
        }

}

See below the image I used for the watermark logo and one example of a generated image using the code above:



Written by smartdev

May 30th, 2011 at 8:56 am