<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Kamal's blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/kamalds/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/kamalds/feed.xml</link>
    <description>Happy coding!!</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:10:41 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Working with EXIF metadata</title>
      <link>https://docs.microsoft.com/archive/blogs/kamalds/working-with-exif-metadata</link>
      <pubDate>Sun, 08 Apr 2012 07:42:00 GMT</pubDate>
      <dc:creator><![CDATA[Kamal Deep Soni]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kamalds/2012/04/08/working-with-exif-metadata/</guid>
      <description><![CDATA[Introduction
Few days back I had a requirement to generate random images, and add&nbsp;EXIF...]]></description>
      <content:encoded><![CDATA[<h2>Introduction</h2>
<p>Few days back I had a requirement to generate random images, and add&nbsp;EXIF metadata to them to test some functionality. It was hard to find images with specfic EXIF metadata, so I decided to write a small code which could take care of this requirement.</p>
<p>This blog gives an idea of adding/updating EXIF metadata&nbsp;of an image, and is based on the Kevin's work. The original post can be found <a title="here" href="https://archive.msdn.microsoft.com/changexifwithcsharp">here</a>.</p>
<h2>Implementation</h2>
<p>The sample code below demonstrates functionality to add (if not exist) and update&nbsp;EXIF metadata. &nbsp;The function <em>WriteExifField </em>actually takes care of adding EXIF data.</p>
<div style="border: 1px solid #000080; width: 100% color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="overflow: auto; background: #ffffff; width: 100%;"><ol style="background: #ffffff; margin: 0px; padding: 0px 0px 0px 5px;" start="322">
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Change/Add standard EXIF field vlaue</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name="file"&gt;&lt;/param&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name="objExifInfo"&gt;&lt;/param&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> ChangeImageExif(<span style="color: #0000ff;">string</span> file, <span style="color: #2b91af;">EXIFInfo</span> objExifInfo)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">#region</span> Vars</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> sPropValue = <span style="color: #0000ff;">string</span>.Empty;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span> field;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">PropertyItem</span> propItem = <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ImageFormat</span> ifOriginal = <span style="color: #0000ff;">null</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Graphics</span> gSave = <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Image</span> iOriginal = <span style="color: #0000ff;">null</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Image</span> iSave = <span style="color: #0000ff;">null</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">#endregion</span></li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">try</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iOriginal = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Bitmap</span>(file);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ifOriginal = iOriginal.RawFormat;</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// For each EXIFField in objExifInfo, add it to Image EXIF</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> exField <span style="color: #0000ff;">in</span> objExifInfo)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;field = (<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span>)<span style="color: #2b91af;">Enum</span>.Parse(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span>), exField.Key.ToString());</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">try</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Get the EXIF value from Image</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propItem = iOriginal.GetPropertyItem((<span style="color: #0000ff;">int</span>)field);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sPropValue = System.Text.<span style="color: #2b91af;">Encoding</span>.UTF8.GetString(propItem.Value);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Change the value</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sPropValue = sPropValue.Replace(sPropValue, exField.Value);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Get bytes</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propItem.Value = System.Text.<span style="color: #2b91af;">Encoding</span>.UTF8.GetBytes(sPropValue);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Set the property on the image</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iOriginal.SetPropertyItem(propItem);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">catch</span> (System.<span style="color: #2b91af;">ArgumentException</span>)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// EXIF tag doesn't exist, add it to image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WriteEXIFField(iOriginal, field, exField.Value.ToString() + <span style="color: #a31515;">"\0"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Store the list of properties that exist on the image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ArrayList</span> alPropertyItems = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ArrayList</span>();</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">PropertyItem</span> pi <span style="color: #0000ff;">in</span> iOriginal.PropertyItems)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alPropertyItems.Add(pi);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Create temp image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iSave = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Bitmap</span>(iOriginal.Width, iOriginal.Height, iOriginal.PixelFormat);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Copy the original image over to the temp image</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gSave = <span style="color: #2b91af;">Graphics</span>.FromImage(iSave);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//If you check iSave at this point, it does not have any EXIF properties -</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//only the image gets recreated</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gSave.DrawImage(iOriginal, 0, 0, iOriginal.Width, iOriginal.Height);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Get rid of the locks on the original image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iOriginal.Dispose();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gSave.Dispose();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Copy the original EXIF properties to the new image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">PropertyItem</span> pi <span style="color: #0000ff;">in</span> alPropertyItems)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iSave.SetPropertyItem(pi);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//Save the temp image over the original image</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iSave.Save(file, ifOriginal);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">catch</span> (<span style="color: #2b91af;">Exception</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// TODO: Exception logging</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">finally</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iSave.Dispose();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Add a standard EXIF field to the image</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name="image"&gt;&lt;/param&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name="field"&gt;&lt;/param&gt;</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name="fieldValue"&gt;&lt;/param&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> WriteEXIFField(<span style="color: #2b91af;">Image</span> image, <span style="color: #2b91af;">ExifField</span> field, <span style="color: #0000ff;">string</span> fieldValue)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Encoding</span> asciiEncoding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ASCIIEncoding</span>();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Text.<span style="color: #2b91af;">Encoder</span> encoder = asciiEncoding.GetEncoder();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">char</span>[] tagTextChars = fieldValue.ToCharArray();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> byteCount = encoder.GetByteCount(tagTextChars, 0, tagTextChars.Length, <span style="color: #0000ff;">true</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] tagTextBytes = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[byteCount];</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encoder.GetBytes(tagTextChars, 0, tagTextChars.Length, tagTextBytes, 0, <span style="color: #0000ff;">true</span>);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (image.PropertyItems != <span style="color: #0000ff;">null</span> &amp;&amp; image.PropertyItems.Length &gt; 0)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">PropertyItem</span> propertyItem = image.PropertyItems[0];</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propertyItem.Id = (<span style="color: #0000ff;">int</span>)field;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propertyItem.Type = 2;&nbsp;&nbsp;<span style="color: #008000;">// ASCII</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propertyItem.Len = tagTextBytes.Length;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;propertyItem.Value = tagTextBytes;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image.SetPropertyItem(propertyItem);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol></div>
</div>
<p>In order to apply multiple EXIF metadata changes to an image, let's use a custom class which inherits from System.Collections.Generic.Dictionary</p>
<p>A simple implementation should look like below</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #ffffff; overflow: auto; max-height: 500px;"><ol style="background: #ffffff; margin: 0px; padding: 0px 0px 0px 5px;" start="445">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">EXIFInfo</span> : <span style="color: #2b91af;">Dictionary</span>&lt;<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span>, <span style="color: #0000ff;">string</span>&gt;</li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Initializes a new instance of the EXIFInfo class.</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> EXIFInfo()&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Default constructor</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Adds an EXIFField with the specified property value into the EXIFInfo </span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">void</span> Add(<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span> key, <span style="color: #0000ff;">string</span> value)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">string</span>.IsNullOrEmpty(value))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ArgumentException</span>(<span style="color: #a31515;">"Value can not be empty"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">base</span>.Add(key, value);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol></div>
</div>
<p>To make this code work, we need to know EXIF tag id.&nbsp;I have added Equipment make and model information for demonstration purpose, but other EXIF fields can be added. A detaild EXIF specification can be found <a title="here" href="https://www.exif.org/specifications.html">here</a>.</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #ffffff; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px; padding: 0px 0px 0px 5px;" start="444">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">enum</span> <span style="color: #2b91af;">ExifField</span> : <span style="color: #0000ff;">int</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;EquipMake = 0x10F,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;EquipModel = 0x110</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> More fields here</span></li>
<li style="background: #f3f3f3;">}</li>
</ol></div>
</div>
<p>Now, we can&nbsp;use ExifInfo to add/update multiple EXIF&nbsp;metadata to an image like following&nbsp;</p>
<div style="border: 1px solid #000080; color: #000000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #ffffff; overflow: auto; max-height: 300px;"><ol style="background: #ffffff; margin: 0px; padding: 0px 0px 0px 5px;" start="49">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DoIt(<span style="color: #0000ff;">string</span> fileName)</li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">EXIFInfo</span> info = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">EXIFInfo</span>();</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Add Exif Info to be added/updated</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;info.Add(<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span>.EquipMake, <span style="color: #a31515;">"Nikon"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;info.Add(<span style="color: #2b91af;">EXIFBase</span>.<span style="color: #2b91af;">ExifField</span>.EquipModel, <span style="color: #a31515;">"SomeMake"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #808080;">///</span><span style="color: #008000;"> Call the main function</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">EXIFBase</span>.ChangeImageExif(fileName, info);</li>
<li>}</li>
</ol></div>
</div>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Chunking large video file with webservice</title>
      <link>https://docs.microsoft.com/archive/blogs/kamalds/chunking-large-video-file-with-webservice</link>
      <pubDate>Thu, 15 Dec 2011 07:56:00 GMT</pubDate>
      <dc:creator><![CDATA[Kamal Deep Soni]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kamalds/2011/12/15/chunking-large-video-file-with-webservice/</guid>
      <description><![CDATA[Introduction
This blog gives an idea of uploading&nbsp;a large video file&nbsp;in chunks, and...]]></description>
      <content:encoded><![CDATA[<h4><span style="font-family: verdana,geneva;">Introduction</span></h4>
<p><span style="font-family: verdana,geneva;">This blog gives an idea of uploading&nbsp;a large video file&nbsp;in chunks, and encrypting chunks as they are received&nbsp;on the server. </span></p>
<h4><span style="font-family: verdana,geneva;">Requirements</span></h4>
<p><span style="font-family: verdana,geneva;">Following are the key requirements</span></p>
<ul>
<li><span style="font-family: verdana,geneva;">Functionality to upload large video files using a webservice</span></li>
<li><span style="font-family: verdana,geneva;">Instead of sending file in a single go, client should divide the&nbsp;file into chunks</span></li>
<li><span style="font-family: verdana,geneva;"></span><span style="font-family: verdana,geneva;">Server should encrypt every chunk received and save </span></li>
</ul>
<p><span style="font-family: verdana,geneva;">Of-course we can propose WCF&nbsp;to upload the data&nbsp;either by&nbsp;using <a title="Streaming large data in WCF" href="https://msdn.microsoft.com/en-us/library/ms733742.aspx">streaming</a> or <a title="Chunking Channel" href="https://msdn.microsoft.com/en-us/library/aa717050.aspx">chunking</a>&nbsp;efficiently, but there might be cases where we need to&nbsp;extend an existing webservice (ASMX) to allow uploading large video files and secure them with minimal changes in design/code.&nbsp;</span></p>
<p><span style="font-family: verdana,geneva;">Considering the fact that video files can be huge, uploading them in a single go is not a preferred approach because of high memory usages while&nbsp;buffering, and yes, there is a limit&nbsp;of the file upload size. While uploading the chunks, we also&nbsp;have to ensure that every chunk is saved securely (encrypted)&nbsp;on the server. Later this file can be sent&nbsp;decrypted to trusted client(s).</span></p>
<h4><span style="font-family: verdana,geneva;">Implementation</span></h4>
<p><span style="font-family: verdana,geneva;">We need a symmetric key (or private-key)&nbsp;which uses a single key to encrypt and decrypt. </span><span style="font-family: verdana,geneva;">The <a title="RijndaelManaged Class" href="https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx">RijndaelManaged</a> class under&nbsp;System.Security.Cryptography namespace can be used to create symmetric key with the <a title="CryptoStream Class" href="https://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream%28v=vs.71%29.aspx">CryptoStream</a> to encrypt&nbsp;the byte array.&nbsp;</span></p>
<p><span style="font-family: verdana,geneva;">For demonstration purpose, let's build a simple win form which will send chunks of a video file, and a web service which will receive these chunks and will encrypt them while saving to the disk. </span><span style="font-family: verdana,geneva;">In order to use the RijndaelManaged class, we can either use the default Key and IV (Initialize vector)&nbsp;provided by class itself, or we can use&nbsp;our own&nbsp;password to generate Key and IV. </span></p>
<p><span style="font-family: verdana,geneva;">Following code can be used to generate encryption Key and IV based on a password string in the webservice</span></p>
<table style="width: 500px;" id="tbl_1" border="0">
<tbody>
<tr>
<td>
<div style="width: 500px; overflow: auto;">
<pre style="font-size: small; color: black; font-family: Consolas,Courier New,Courier,Monospace; background-color: #ffffff;"><span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Password string</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">string</span> password = <span style="color: #006080;">"some_password"</span>;
       
<span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Generates encryption Key based on the password</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">/// &lt;param name="keySize"&gt;&lt;/param&gt;</span>
<span style="color: #008000;">/// &lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">byte</span>[] GetEncryptionKey(<span style="color: #0000ff;">int</span> keySize)
{
    <span style="color: #00ace6;">Rfc2898DeriveBytes</span> key = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">Rfc2898DeriveBytes</span>(password,<br /> 			<span style="color: #00ace6;"> Encoding</span>.ASCII.GetBytes(password.ToCharArray()));
    <span style="color: #0000ff;">return</span> key.GetBytes(keySize / 8);           
}

<span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Generates encryption IV based on the password</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">/// &lt;param name="blockSize"&gt;&lt;/param&gt;</span>
<span style="color: #008000;">/// &lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">byte</span>[] GetEncryptionIV(<span style="color: #0000ff;">int</span> blockSize)
{
    <span style="color: #00ace6;">Rfc2898DeriveBytes</span> key = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">Rfc2898DeriveBytes</span>(password, 
                  <span style="color: #00ace6;"> Encoding</span>.ASCII.GetBytes(password.ToCharArray()));
    <span style="color: #0000ff;">return</span> key.GetBytes(blockSize / 8);
}</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p>Okay, let's&nbsp;use above Key and IV and write the webmethod which will receive chunks and will encrypt them. The webmethod has only one parameter of type UploadRequest. The implementaiton of UploadRequest class can be found in the sample code.</p>
<table style="width: 682px; height: 505px; overflow: auto;" id="tbl_2" border="0">
<tbody>
<tr>
<td>
<div style="width: 500px; overflow: auto;">
<pre style="font-size: small; color: black; font-family: Consolas,Courier New,Courier,Monospace; background-color: #ffffff;"><span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Receives chunks of video file, encrypt them and save to disk</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">/// &lt;param name="objRequest"&gt;Instance of class UploadRequest&lt;/param&gt;</span>
[<span style="color: #00ace6;">WebMethod</span>]        
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> UploadVideo(<span style="color: #00ace6;">UploadRequest</span> objRequest)
{
    <span style="color: #0000ff;">long</span> length = 0;
    <span style="color: #0000ff;">string</span> tempUploadLocation = <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"C:\\temp\\{0}"</span>, <br /><span style="color: #00ace6;"> Path</span>.GetFileName(objRequest.FileName));

    <span style="color: #0000ff;">if</span> (objRequest.FileBytes.Length &gt; 0)
    {
        <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">FileStream</span> writer = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">FileStream</span>(tempUploadLocation, <br /><span style="color: #00ace6;"> File</span>.Exists(tempUploadLocation) ? <span style="color: #00ace6;">FileMode</span>.Append : <br /><span style="color: #00ace6;"> FileMode</span>.Create, <span style="color: #00ace6;">FileAccess</span>.Write))
        {
            <span style="color: #00ace6;">RijndaelManaged</span> rm = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">RijndaelManaged</span>();
            <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">CryptoStream</span> crStream = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">CryptoStream</span>(writer, <br />		   			   rm.CreateEncryptor(<span style="color: #00ace6;">EncryptionHelper</span>.GetEncryptionKey(rm.KeySize),
                   			   <span style="color: #00ace6;">EncryptionHelper</span>.GetEncryptionIV(rm.BlockSize)), <span style="color: #00ace6;">CryptoStreamMode</span>.Write))
            {
                crStream.Write(objRequest.FileBytes, 0, objRequest.FileBytes.Length);
                length = writer.Length;
            }
        }
    }
    <span style="color: #008000;">//// Check if file upload is complete</span>
    <span style="color: #0000ff;">if</span> (length &gt;= objRequest.FileSize)
    {
        <span style="color: #008000;">//// Post upload processing goes here </span>
    }           
}</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p>To decrypt the video, we need same symmetric key. Following code can be used to decrypt the video&nbsp;</p>
<table id="tbl_4" border="0">
<tbody>
<tr>
<td>
<div style="width: 500px; overflow: auto;">
<pre style="color: black; font-family: Consolas,Courier New,Courier,Monospace; font-size: small; background-color: #ffffff;"><span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Decrypt the file and save it to the temp location (Server)</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">/// &lt;param name="filePath"&gt;&lt;/param&gt;</span>
[<span style="color: #00ace6;">WebMethod</span>]
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> DecryptVideoAndSaveInTemp(<span style="color: #0000ff;">string</span> filePath)
{
    <span style="color: #0000ff;">byte</span>[] bytMedia = <span style="color: #0000ff;">null</span>;
    <span style="color: #0000ff;">string</span> tempDownloadLocation = <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"D:\\Temp\\{0}"</span>,Path.GetFileName(filePath));

    <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">FileStream</span> fs1 = <span style="color: #00ace6;">File</span>.Open(filePath, <span style="color: #00ace6;">FileMode</span>.Open, <span style="color: #00ace6;">FileAccess</span>.Read))
    {                
        <span style="color: #00ace6;">RijndaelManaged</span> rm = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">RijndaelManaged</span>();
        rm.Padding = <span style="color: #00ace6;">PaddingMode</span>.None;

        <span style="color: #008000;">//// Decrypt the file</span>
        <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">CryptoStream</span> cs = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">CryptoStream</span>(fs1, rm.CreateDecryptor(<span style="color: #00ace6;">EncryptionHelper</span>.GetEncryptionKey(rm.KeySize), <span style="color: #00ace6;">EncryptionHelper</span>.GetEncryptionIV(rm.BlockSize)), <span style="color: #00ace6;">CryptoStreamMode</span>.Read))
        {
            bytMedia = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[fs1.Length];
            Stream streamObject = cs;
            streamObject.Read(bytMedia, 0, (<span style="color: #0000ff;">int</span>)fs1.Length);
        }

        <span style="color: #008000;">//// Use the decrypted byte array and save to the disk</span>
        <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">FileStream</span> fs = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">FileStream</span>(tempDownloadLocation, <span style="color: #00ace6;">FileMode</span>.CreateNew))
        {
            fs.Write(bytMedia, 0, bytMedia.Length);
        }                
    }

    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"File decrypted and copied to {0}"</span>, tempDownloadLocation);
}</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p>By now our webservice is ready to receive chunks of the file, now we need a client which will actually divide the video into chunks, and will call the UploadVideo webmethod for each chunk.</p>
<p>The chunkSize&nbsp;(set to 500KB)&nbsp;can be a key factor to improve performance of the upload based upon the factors like network speed, server resources etc.&nbsp;</p>
<table style="width: 500px; height: 505px; overflow: auto;" id="tbl_3" border="0">
<tbody>
<tr>
<td>
<div style="width: 500px; overflow: auto;">
<pre style="font-size: small; color: black; font-family: Consolas,Courier New,Courier,Monospace; background-color: #ffffff;"><span style="color: #008000;">/// &lt;summary&gt;</span>
<span style="color: #008000;">/// Chunk the file and upload</span>
<span style="color: #008000;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">/// &lt;param name="filename"&gt;&lt;/param&gt;</span>
<span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> UploadVideo(<span style="color: #0000ff;">string</span> filename)
{
    <span style="color: #cc6633;">#region</span> Vars
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> chunkSize = 512000;
    <span style="color: #0000ff;">byte</span>[] bytes = <span style="color: #0000ff;">null</span>;
    <span style="color: #0000ff;">int</span> startIndex, endIndex, length, totalChunks;           

    WS.UploadRequest objRequest = <span style="color: #0000ff;">new</span> WS.UploadRequest();            
    <span style="color: #cc6633;">#endregion</span>

    <span style="color: #0000ff;">try</span>
    {
        <span style="color: #0000ff;">if</span> (<span style="color: #00ace6;">File</span>.Exists(filename))
        {
            <span style="color: #0000ff;">using</span> (WS.<span style="color: #00ace6;">UploadService</span> objService = <span style="color: #0000ff;">new</span> WS.<span style="color: #00ace6;">UploadService</span>())
            {
                <span style="color: #0000ff;">using</span> (<span style="color: #00ace6;">FileStream</span> fs = <span style="color: #0000ff;">new</span> <span style="color: #00ace6;">FileStream</span>(filename, <span style="color: #00ace6;">FileMode</span>.Open, <span style="color: #00ace6;">FileAccess</span>.Read, <span style="color: #00ace6;">FileShare</span>.Read))
                {
                    <span style="color: #008000;">//// Calculate total chunks to be sent to service</span>
                    totalChunks = (<span style="color: #0000ff;">int</span>)<span style="color: #00ace6;">Math</span>.Ceiling((<span style="color: #0000ff;">double</span>)fs.Length / chunkSize);

                    <span style="color: #008000;">//// Set up Upload request object</span>
                    objRequest.FileName = filename;
                    objRequest.FileSize = fs.Length;
                            
                    <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; totalChunks; i++)
                    {
                        startIndex = i * chunkSize;
                        endIndex = (<span style="color: #0000ff;">int</span>)(startIndex + chunkSize &gt; fs.Length ? fs.Length : startIndex + chunkSize);
                        length = endIndex - startIndex;
                        bytes = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[length];

                        <span style="color: #008000;">//// Read bytes from file, and send upload request</span>
                        fs.Read(bytes, 0, bytes.Length);
                        objRequest.FileBytes = bytes;
                        objService.UploadVideo(objRequest);
                    }
                }

            }
        }
    }
    <span style="color: #0000ff;">catch</span> (<span style="color: #00ace6;">Exception</span> ex)
    {
        <span style="color: #00ace6;">MessageBox</span>.Show(<span style="color: #0000ff;">string</span>.Format(<span style="color: #006080;">"Error- {0}"</span>, ex.Message));
    }
}</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Is it limited to video files only?</h4>
<p>No, this technique is not limited to video files, but the sample code is not tested with other file types.</p>
<p>&nbsp;</p>
<p>The sample code can be downloaded from <strong><a title="Sample Code" href="https://skydrive.live.com/redir.aspx?cid=49ea1deb771f5a19&amp;resid=49EA1DEB771F5A19!703&amp;parid=root" target="_blank">here</a></strong>.</p>]]></content:encoded>
    </item>
  </channel>
</rss>