Archive
Capture hi-res image from iTunes using C#
This is a quick demo of how to use the iTunes API to extract the hi-res icon of any iOS app;
WebClient wc = new WebClient();
string strUrl = @”http://itunes.apple.com/search?term=” + HttpUtility.UrlEncode(tbSearch.Text) + “&entity=iPadSoftware”;
string strHtml = wc.DownloadString(strUrl);
const string strArtRegex = “artworkUrl100.:\”(.*?)\””;
Match mArt = Regex.Match(strHtml, strArtRegex);
string Url = mArt.Groups[1].Value;
imgArtwork.Src = Url;
This assumes a user interface somewhat like this:
<asp:TextBox runat=”server” ID=”tbSearch”></asp:TextBox>
<asp:Button runat=”server” ID=”btnSearch” OnClick=”btnSearchClick” Text=”Search”/>
<br/>
<img id=”imgArtwork” runat=”server”/>
Hope this helps someone.
Favourite Tweets using C# Tweetsharp
If you want to make noise on twitter, favouriting people’s tweets does draw a little attention, so I decided to try to do this automatically, to see if I could build followers.- Here is the code I used in C# (authentication details removed) – you need a reference to TweetSharp
var service = new TwitterService(“########”, “######”);
service.AuthenticateWith(“###”, “#####”);
TwitterSearchResult res = service.Search(new SearchOptions { Q = args[0], Count = 100});
IEnumerable<TwitterStatus> status = res.Statuses;
foreach (var tweet in status)
{
Console.WriteLine(tweet.Text);
service.FavoriteTweet(new FavoriteTweetOptions{Id = tweet.Id});
}
It takes a command line argument, which it uses as a search term, so you can favourite tweets that are relevant.
And, hey if you want to follow me, please go to: https://twitter.com/webtropy
2014 in review
The WordPress.com stats helper monkeys prepared a 2014 annual report for this blog.
Here’s an excerpt:
The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 50,000 times in 2014. If it were a concert at Sydney Opera House, it would take about 19 sold-out performances for that many people to see it.
Read extended properties of a file in C#
You need to make a reference to the COM library “Microsoft Shell controls and automation”
[STAThread]
public static void Main(string[] args)
{
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
var strFileName = @”C:\Users\Fiach\Desktop\sample_iTunes.mov”;
Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName));
Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName));
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine(“{0}\t{1}: {2}”, i, arrHeaders[i], objFolder.GetDetailsOf(folderItem, i));
}
Console.ReadLine();
}
HTTP POST using PHP
$url = ‘https://posttestserver.com/post.php’;
$data = array(‘key1’ => ‘value1’, ‘key2’ => ‘value2’);
// use key ‘http’ even if you send the request to https://…
$options = array(
‘http’ => array(
‘header’ => “Content-type: application/x-www-form-urlencoded\r\n”,
‘method’ => ‘POST’,
‘content’ => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo($result);
—- And the cool thing is that posttestserver.com lets you see what you posted to it. cool service!
WebOS app store apps released for free
With the demise of the @palm #webos app store today, We’re now giving away all out webos apps for free, download from http://1drv.ms/1tvPIWH
That’s 31 apps that we developed, all gone for free, to new and better homes…
JQuery, Binding on nested element
In this simple example, there is a nasty bug
<html>
<head>
<script src=”http://code.jquery.com/jquery-1.11.0.min.js”></script>
</head>
<body>
<div id=”outer” style=”background:red; width:400px; height:200px”>
<div id=”inner” style=”background:blue; width:200px; height:100px”>
</div>
</div>
<script>
$(init);
function init()
{
$(“#outer”).bind(“click”, outer_click);
$(“#inner”).bind(“click”, inner_click);
}
function outer_click(event){
alert(“outer click”);
}
function inner_click(event){
alert(“inner click”);
}
</script>
</body>
</html>
If you click on the blue square, then you get two alerts, one for the inner element, and one for the outer element.
The solution is to stop event bubbling, by including this on the inner_click
event.stopPropagation();
A Hello World example for Foundation Reveal
Foundation is a Javascript / CSS framework similar to Bootstrap, and I was looking at the Reveal plugin to create modal dialogs, but dismayed when there was no really simple “hello world” example.
First, download Foundation “complete”, and then create a HTML file with this content
<html>
<head>
<link href=”css/foundation.css” rel=”stylesheet” />
<script src=”js/vendor/modernizr.js”></script>
<script src=”js/vendor/jquery.js”></script>
<script src=”js/foundation/foundation.js”></script>
<script src=”js/foundation/foundation.reveal.js”></script>
</head>
<body><a href=”#” id=”btnJoin” >Join</a>
<div id=”dialogPage” class=”reveal-modal” data-reveal>
<h2>Alert</h2>
<p id=”pMessage”></p>
<a class=”close-reveal-modal”>×</a>
</div><script>
$(init);
function init()
{
$(document).foundation();
$(“#btnJoin”).bind(“click”, btnJoin_click);
}function btnJoin_click()
{
jqmAlert(“Hey, I’m an alert”);
}function jqmAlert(message)
{
console.log(“jqmAlert(” + message + “)”);
$(“#pMessage”).html(message);
$(‘#dialogPage’).foundation(‘reveal’, ‘open’);
}
</script>
</body>
</html>
Create thumbnail from remote image in c#
Most of the thumbnail scripts I came across show how to create a thumbnail from a local image, but if the image is on another server, then here’s a script in asp.net / c# for that:
protected void Page_Load(object sender, EventArgs e)
{
string Image = Request.QueryString[“Image”];
if (Image == null)
{
throw new Exception(“Image is required”);
}
string sSize = Request[“Size”];
int Size = 120;
if (sSize != null) Size = Int32.Parse(sSize);
Bitmap bmp = CreateThumbnail(Image, Size, Size);
// Put user code to initialize the page here
Response.ContentType = “image/jpeg”;
bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
}
Then we add the code for CreateThumbnail;
public static Bitmap CreateThumbnail(string strUrl,int lnWidth, int lnHeight)
{System.Drawing.Bitmap bmpOut = null;
System.Net.WebRequest request = System.Net.WebRequest.Create(strUrl);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
Bitmap loBMP = new Bitmap(responseStream);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal) lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal) lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int) lnTemp;
}
// System.Drawing.Image imgOut =
// loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
// null,IntPtr.Zero);// *** This code creates cleaner (though bigger) thumbnails and properly
// *** and handles GIF files better by generating a white background for
// *** transparent images (as opposed to black)
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle( Brushes.White,0,0,lnNewWidth,lnNewHeight);
g.DrawImage(loBMP,0,0,lnNewWidth,lnNewHeight);
loBMP.Dispose();
return bmpOut;
}
I’ll probably be developing this further to implement a local cache of thumbnails, but let’s see how this goes…
Handle custom Url schemes in Chrome / Firefox
If you want to be the go-to site to handle common url schemes (protocols), such as a Gmail wants to handle mailto: links, and so forth, then you can prompt the user to accept your website as the default handler for common uri schemes, using this simple code:
try
{
navigator.registerProtocolHandler(“protocol”, “http://www.yourdomain.com/?to=%s”, “Your site”);
} catch (ex) { }
The Try / Catch is just there for older browsers, or Internet explorer. The script has to be executed from the same domain as mentioned in the protocol handler. Otherwise it’ll be just flatly ignored.
