Google.Api.Ads.AdWords.Examples.CSharp.v201306.UploadImage.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
return void
        public void Run(AdWordsUser user)
        {
            // Get the MediaService.
              MediaService mediaService = (MediaService) user.GetService(
              AdWordsService.v201306.MediaService);

              // Create the image.
              Image image = new Image();
              image.data = MediaUtilities.GetAssetDataFromUrl("http://goo.gl/HJM3L");
              image.type = MediaMediaType.IMAGE;

              try {
            // Upload the image.
            Media[] result = mediaService.upload(new Media[] {image});

            // Display the results.
            if (result != null && result.Length > 0) {
              Media newImage = result[0];
              Dictionary<MediaSize, Dimensions> dimensions =
              CreateMediaDimensionMap(newImage.dimensions);
              Console.WriteLine("Image with id '{0}', dimensions '{1}x{2}', and MIME type '{3}'" +
              " was uploaded.", newImage.mediaId, dimensions[MediaSize.FULL].width,
              dimensions[MediaSize.FULL].height, newImage.mimeType);
            } else {
              Console.WriteLine("No images were uploaded.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to upload image.", ex);
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     UploadImage codeExample = new UploadImage();
       Console.WriteLine(codeExample.Description);
       try {
     codeExample.Run(new AdWordsUser());
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }