Text to image in ASP.net C#
Sounds fun? Well it was fun to me at least. Before I forget that I even did this, I thought I’d write about it.
1. Create a bitmap image
2. Create a graphics object from Bipmap image
3. Draw text using graphics object onto that bitmap image
4. Save bitmap image into desired format using Response.outputstream
This will directly send the image to the browser.
What’s the use ?
* Image of text on demand. You can use <img src=”http://location/?text=hello” />
* more parameters you can add to render text on different fonts or do any karate you wish
* or whatever comes to your mind ….
Code: which you’d put on Page_Load
Bitmap objBMP;
Graphics objGraphics;
Font objFont;
int height =30
int width = 200
Color bgcolor = Color.White;
string fontname ="Arial";
int fontsize = 10;
FontStyle fontstyle = FontStyle.Regular;
string input = "Sample Text"
Brush brushColor = Brushes.Black;
int textX = 0;
int textY = 3;
ImageFormat imgFormat = ImageFormat.Jpeg;
objBMP = new System.Drawing.Bitmap(width, height);
objGraphics = Graphics.FromImage(objBMP);
objGraphics.Clear(bgcolor);
objGraphics.TextRenderingHint = TextRenderingHint.SystemDefault;
objFont = new Font(fontname, fontsize, fontstyle);
objGraphics.DrawString(input, objFont, brushColor, textX, textY);
Response.ContentType = "image/jpeg";
objBMP.Save(Response.OutputStream, imgFormat);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
Given digits and values, find suitable expression
/* Write a function that given a string of digits and a target value, prints where to put +'s and *'s between the digits so they combine exactly to the target value. Note there may be more than one answer, it doesn't matter which one you print. Examples: "1231231234",11353 -> "12*3+1+23*123*4" "3456237490",1185 -> "3*4*56+2+3*7+490" "3456237490",9191 -> "no solution" program made in windows / cygwin g++ with Netbeans IDE */ #include <iostream> #include <string> #include <math.h> #include <vector> using namespace std; string findExprSymbols(int n, int pos) { string s = "+*-"; int rem = pos; int div = 1; string ret = ""; for(int j=0;j<n;j++) { div = (int)pow(3,n-(j+1)); int index = rem / div ; rem = rem % div; ret += s.at(index); } return ret; } string getExpres(string digits,string expression,int & value) { string exp=""; exp= exp + digits.at(0); string current = exp; vector<string> mystack; int last = 0; for(int i=0;i<expression.size();i++) { switch(expression.at(i)) { case '+': exp= exp + '+'+digits.at(i+1); mystack.push_back(current); mystack.push_back("+"); current= digits.at(i+1); break; case '-': exp= exp + digits.at(i+1); current = current + digits.at(i+1); break; case '*': exp= exp + '*'+digits.at(i+1); mystack.push_back(current); mystack.push_back("*"); current= digits.at(i+1); break; } } mystack.push_back(current); vector<int> evalstack; int j=mystack.size()-1; int temp; for(;j>=0;j--) { string str = mystack.at(j); if(str=="*") { temp = evalstack.back(); temp = atoi(mystack.at(j-1).c_str()) * temp; evalstack.pop_back(); evalstack.push_back(temp); j--; } else if( str == "+") { temp = atoi(mystack.at(j-1).c_str()); evalstack.push_back(temp); j--; } else{ temp = atoi(str.c_str()); evalstack.push_back(temp); } } int final = 0; for(int x=0;x<evalstack.size();x++) { final+=evalstack.at(x); } value = final; //cout<<final<<endl; return exp; } int main(int argc, char** argv) { string digits ; int result; cin>>digits; cin>>result; //string digits="1231231234"; int value; double total = pow(3,digits.size()-1); bool done=false; for(int i=0;i<total;i++) { string expr = findExprSymbols(digits.size()-1,i); string myexp = getExpres(digits,expr,value); if( value == result) { cout<<myexp<<"="<<value<<endl; done = true; break; } } if(!done) { cout<<" no solution"<<endl; } return (EXIT_SUCCESS); }
Python and the cloud
Day 3: Previously, I tried web.py. It’s cool. But my hosting does not provide python CGI support. Now what? I had heard that google’s cloud thing –> google app engine , that supports python. So I gave it a try,
- created google app engine account
- ran thru the “getting started”
Now, it hosts a python application.
The cool part is, it runs on google app engine also known as GAE ( does not sound so good
)
They have their own data store, the DJango like data model, GQL for accessing and querying the data, cool admin interface to do lot of things.
So I made this app http://hamro-app.appspot.com from the tutorials.
Now, later I found out, which is quite exciting for me, is that they support custom domain names, like i can use my own domain name to host the app, so the same app can be accessed through http://hamro-app.bipins.net . Its just a test app though. So, now I have python powered, highly scalable, web application / service. Thanks to app engine.
Internet notepad cl1p.net vs notes.bipins.net
cl1p.net is a well known internet notepad. It has great features and use. Comes very handy and useful.
But I felt I can make that thing too
. So I made it.
http://notes.bipins.net (beta)
Integrated with TinyMCE.
One can:
1. Save and Retrive
Its on beta and I’ve used SQLite for quick POC ( proof of concept ). I’ll migrate it to MSSQL database soon) Currently text size limitation is 4000 char. After migration to MSSQL I’ll increase it to text –> 2 ^ 31 characters.
To be done :
1. Password protect documents
2. Export to .pdf
3. Export to .doc
Please send your suggestions to mail [at] bipins.net
The python thingy
Python guru used to tell me python is nice, python is easy. I should have listened to him then. But anyway now i felt I had time and I can or I shoud? ( should I ?). I should have actually. At least I should have tried to understand the “user creation script” made by the genious boy . I thought its not my thing, but as I can feel now, its quite interesting. Anyway :
Day One: Python Crash course : http://hetland.org/writing/instant-python.html
Then what? Then I asked the Guru again and he suggested me few. He gave me links to web.py
Day Two: http://webpy.org/
Now, I understood how the genious kid made his server in python and the app thing he had been running at his VPS. I wanted a server too
. But my hosting provider Reliablesite.net does not support python. Anyways, no probs, I can do it my way. Hosted on my own machine to test http://python.bipins.net points to my router and then to my laptop
Then what? I asked the Guru again. He said to learn about dictionaries, web.py , mysql, meta programming
. He had worked in some porting of python to .net or vice versa or dunno something like that. Too much to chew. I chewed just a little bit, web.py thing was good enough for me.
Was trying to configure apache and mod_python , dunno what went wrong. Anyway , tried then in IIS and holy coolness, it worked ….. yippie .. followed this link http://net4geeks.com/index.php?option=com_content&task=view&id=48&Itemid=1
and the test app run succesfully at http://python.bipins.net/python/python.py
Now what ?
same question repeats again and again. postponed the question to few days later. Even postponed farmville for few days and planted watermelons, takes 4 days to grow
. Will write about it later.
Non Recursive approach to generate possible combinations of characters in password
Q). Given a password : Write an algorithm to print all possible combinations of that password.
/* A non recursive approach to the problem and similar problems which has to generate permutation of certain set of characters. The idea here is to first find the number of possible combinations of output. Say, n is the length : possible combination = n! Then to find the formula that would repeat the pattern. The recursive method broken down into linear/ incremental formula. The recursion is converted to a loop of size n!. A technique should be generated that could give us the combination output given any instance of value of loop index. Say if n=4, n! = 24. So at any instance, say when loopindex = 11 then it should give the sequence that would be generated on 11th position. For that, we find out position of each character in the remaining set. say, for given "1234" 1st char will be at position index / (n-1)! = 11/ 6 = 1 , so first char = 2; now the given set should exclude the output char => remaining "134" now in the remaining set , the index also decreases the decrement of index , ie. 11 will be = index % (n-1)! = 11 % 6 = 5 Again, index / (n-1)! = 5 / (3-1)! = 0 , so char = 1 remaining set = "34" and so on It can be used for any amount of number or characters. */ #include<iostream> #include<string> using namespace std; int facto(int x) { int fact=1; while(x>0) fact*=x--; return fact; } int main(int argc, char **argv) { string s; int len,fact,rem=0,pos=0; cin>>s; // Input is take as string, it can be checked if it is a number or not, I'm just making it generic len=s.size(); fact = facto(len); // Main loop for(int i=0;i<fact;i++) { string str = s; rem = i; cout<<i+1<<". "; for(int j=len;j>0;j--) { int div = facto ( j-1); pos = rem / div; // finding out position rem = rem % div; // adjusting the index value cout<<str.at(pos); // output char str = str.erase(pos,1); // decreasing the problem set size } cout<<endl; } }
implementing printf()
How would i implement my own printf() ?
well, using stdarg.h we could create a function that can take any number of parameters. Then just parsing through each argument and writing it into standard output.
http://www.careercup.com/question?id=139667&form=comments
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include<string.h>
#define STDOUT 1
#define MAX_INT_LEN 10
//function foo taken from manpages of stdarg
void Printf(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch (*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
write(STDOUT, s, strlen(s));
break;
case 'd': /* int */
d = va_arg(ap, int);
s = malloc(MAX_INT_LEN);
sprintf(s, "%d", d); // lame attempt to convert int to string, no itoa() in linux
write(STDOUT, s, strlen(s));
free(s);
break;
case 'c': /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
write(STDOUT, &c, 1);
break;
}
va_end(ap);
}
int main() {
Printf("%s %d %c", "hello world", 123456 , 's');
}
Setting up SVN in windows within few minutes
Setting up a SVN server had never been so easy as it is now with Visual SVN
If you do not know why to use SVN or what to use SVN for please read this or else follow the steps :
1. Download and install Visual SVN from
http://www.visualsvn.com/server/download/
its free.
2. Create a user.
3. Create a repository.
4. Install Tortoise SVN windows client.
5. Import your code / files into the repository . As per your configuration it would be :
https://yourmachinename/svn/< repository name>
6. Its ready
.
To use it :
1. On any folder where you want to download the code, right click and click on “checkout”.
2. Give the url that is generated ( to find the URL if you forgot, right click on repository and click on properties )
3. give username and password of the user.
4. Click on OK.
To access it from Netbeans.
1. From Netbeans 6.0 onwards they have default support for svn. Just download and install svn client like tortoise svn.
2. Give path to the svn.exe in default path.
3. Open Netbeans, Versioning –> Subversion –> Checkout . ( you might get errors if it cannot find svn.exe on its path).
4. Give the URL of your subversion repository, username and password.
5. Click on Finish
.
To access from Visual Studio :
1. Visual SVN provides a paid plugin for visual studio to use svn, but since we like free ones only. So get the free one “Ankhs SVN” here which works for Visual studio 2005 and Visual studio 2008 both.
2. File –> Open –> Subversion Project
3. Give the svn URL, username password as it asks.
4. Select the solution and open it.
OR
you can also create your project / solution first from visual studio and then commit it to your repository:
a. By right clicking the project / solution
b. Add the selected project/solution into subversion
c. Give the project name, SVN URL and path where to save.
d. That’s all
To Setup SVN in linux please follow Amit Regmi’s blog at http://regamit.blogspot.com/2008/09/svn-setup-linux-server-linux-user.html
website visitor’s country name image
As one of the use of the ip to country service I've used that service to generate the country name image using the text returned from the country name returned from the web page visitor's IP.
It would look like this .
you can see a use here.
To use this you can just add the following in any html page.
<img src="http://countryname.bipins.net" />
This is a basic implementation , i will add more customizations on the image to make it look better later.
Please comment on this post or email at mail@bipins.net for comments and requests for images of different format, font size, color etc.
IP to country web service
I just started a new xml web service to convert IP to country name.
Given
It is availabe at
http://webservices.bipins.net/iptocountry/Service.asmx
Primary function availabe right now is
public string GetCountryName(string ip,string authkey)
It can be used basically for logging in the country name of the visitor.
For now "authkey" field can be kept as blank .
the following example can also be downloaded here.
Example :
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Data.SqlClient;
5: using net.bipins.webservices;
6: namespace ConsoleApplication1
7: {
8: class Program
9: {
10: static void Main(string[] args)
11: {
12: Service svc = new Service();
13: Console.WriteLine(svc.GetCountryName("208.68.171.66", ""));
14: Console.ReadLine();
15: }
16: }
17: }