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
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.
Internet in windows mobile (WM6) emulator , visual studio
While building my first windows mobile application that uses my web services. I had misconception that the windows mobile emulator that comes along with wm6 sdk or the default pocketpc emulators in visual studio would have the internet connection. But sadly it did not worked directly. After googling about the issue for an hour, somewhere someone mentioned that we should
INSTALL VIRTUAL PC to have the internet connection for the windows mobile emulator. I don't know why they have created such a dependency to have such virtual PC networking drivers requirement for the emulator to work.
If anyone who are working on standalone pocketpc application , they would not face such problems, or most of the VS.net developer machines have a virtual PC, which I had previously so it never strike my mind.
Its wiered but anyway after installing virtual PC , it just worked like magic.
Windows SharePoint Services on Vista
Sharepoint seemed to be one of the hot thing to know on .net market. So thought of learning it. After downloading the server from MSDNAA account, tried to install it but it did not get installed. Later I found out that it needs a server to get installed, how obvious. I remember back in previous office they used to have 2003 server for the sharepoint intranet portal. Then I came across this http://www.harbar.net/archive/2007/02/25/Office-SharePoint-Server-Developer-Edition.aspx but found out that its just a request to Microsoft and no such things have been released yet. Sad
. Then going through all the comments someone posted at the end of the comment list about this http://community.bamboosolutions.com/blogs/bambooteamblog/archive/2008/05/21/how-to-install-windows-sharepoint-services-3-0-sp1-on-vista-x64-x86.aspx which seems to be quite convincing one. I finally completed the installation and i can see as they have told. The "enable basic authentication" one is really important coz i missed it and the page was blank
. I guess now i can move ahead withthe SDK from this point.
.Net for java programmers
I had came across this post long time back which talks about .Net for Java programmers. It is quite helpful for the transition of java programmers to .net.