extension Int {
func toColor() -> UIColor {
let r = self % 256
let g = ((self - r) % 65536) / 256
let b = (self - g) / 65536
return UIColor(red: Float(r), green: Float(g), blue: Float(b), alpha: 1)
}
}
23 July 2014
Math - Extract the RGB values from a long color
This is a short little peace of code to extract the RGB components of a color stored in a single long value – like they are in Visual Basic. This code is in Apple's Swift language, but that really doesn't matter. You just need to know it's an extension on the Int type and "self" refers to the variable containing the long color.
08 July 2014
C# - Bits to connect to a database
The following are the basic methods to retrieve data from and store data in a database using C#.
The connection string
In a web application, we pick this up in the web.config as follows:private static string ConnectionString {
get {
var cs = ConfigurationManager.ConnectionStrings["ConnStrConsult"];
if (cs != null)
return cs.ConnectionString;
else
throw new Exception("Connection string not found.");
}
}
Retrieve data
This method takes an SQL SELECT statement and an optional number of parameters that are used in this statement. Also see LINQ on datatable for further processing of the retrieved data and Retrieve objects to retrieve an IEnumerable of model objects instead.private static DataTable Retrieve(string query, params object[] parameters) {
try {
var data = new DataTable();
using (var adapter = new OdbcDataAdapter(query, ConnectionString)) {
SetParameters(adapter.SelectCommand, parameters);
adapter.Fill(data);
}
return data;
}
catch (OdbcException ex) {
if (ex.Message.Contains("database not found"))
throw new Exception("Could not find database.");
else
throw;
}
}
private static void SetParameters(OdbcCommand command, params object[] parameters) {
if (command != null && parameters != null && parameters.Length > 0) {
int i = 0;
while (i < parameters.Length) {
command.Parameters.AddWithValue("p" + i, parameters[i] ?? DBNull.Value);
i++;
}
}
}
Execute statements
This one takes an INSERT, UPDATE or DELETE statement with optional parameters and executes it on the database.private static object Execute(string query, params object[] parameters) {
using (var connection = new OdbcConnection(ConnectionString))
using (var command = new OdbcCommand(query, connection)) {
SetParameters(command, parameters);
connection.Open();
return command.ExecuteScalar();
}
}
02 June 2014
Windows XP - Cannot validate SSL TCP connection
While I was working on my email client in .net, using a TcpClient, I tested it on a computer that ran Windows XP SP2, and it failed to connect to the IMAP servers. It always returned the error The remote certificate is invalid according to the validation procedure. It worked fine on a Windows 7 laptop.
It turned out that the certificate could not be validated on an XP SP2 machine because it doesn't have the updated cryptography API called Cryptography Next Generation or CNG.
The only solution for me was to update to XP SP3. Once installed, I ran the email client again and it all worked perfectly.
22 May 2014
C# - Asynchronious methods with a callback
I thought this one up as a nice alternative to custom events: a method that does it's work asynchroniously and that invokes a callback method when it's done.
The method
public void GetMailBoxes(Action<Exception, List<MailBox>> method) {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (object sender, DoWorkEventArgs e) => {
e.Result = Imap.Instance.GetMailBoxes();
};
worker.RunWorkerCompleted += (object sender, RunWorkerCompletedEventArgs e) => {
method(e.Error, mMailBoxes);
};
worker.RunWorkerAsync();
}
Calling the method
account.GetMailBoxes((Exception ex, ListmailBoxes) => { if (ex != null) { Program.Handle(ex); } else { lstMailBoxes.DataSource = mailBoxes; } });
05 May 2014
IMAP - Retrieving data from a mail box
IMAP is a protocol for a client to manage a mail box on a server. The following .net code connects to a mail server through IMAP and retrieves data from it:
Connect
This opens a secured TCP connection to the server.
StringBuilder result = new StringBuilder();
TcpClient connection = new TcpClient("imap.telenet.be", 993);
SslStream stream = new SslStream(connection.GetStream(), false);
stream.AuthenticateAsClient("imap.telenet.be");
StreamWriter writer = new StreamWriter(stream);
StreamReader reader = new StreamReader(stream);
result.AppendLine(reader.ReadLine());
Login
This logs you in. Always flush the command to actualy send it. All commands are sent in this fashion. Notice how each command starts with a different tag to identify it. These are usually generated with an auto-increment.
writer.WriteLine(@"e001 LOGIN myemail@telenet.be mypassword");
writer.Flush();
CheckResponseOk("Login");
CheckResponseOk
This function retrieves the response on a command and checks whether it is possitive. This is for commands that don't return data, like connect and login. These return "OK" if they succeed.
private void CheckResponseOk(string context) {
string[] response = GetResponse();
if (!response.Last().Contains("OK"))
throw new ApplicationException(context + " failed:\n" + string.Join("\n", response));
}
GetResponse
This function retrieves the response on a command. A response can be multiple lines. The last line always starts with the tag that was sent in the command.
private string[] GetResponse(int logLines = -1) {
List result = new List();
string tag = GetTag(false);
string line = mReader.ReadLine();
int loggedLines = 0;
while (!line.StartsWith(tag)) {
result.Add(line);
if (logLines < 0 || loggedLines < logLines) {
mLogWriter.WriteLine(line);
loggedLines++;
}
line = mReader.ReadLine();
}
result.Add(line);
if (logLines < 0 || loggedLines < logLines) {
mLogWriter.WriteLine(line);
loggedLines++;
}
return result.ToArray();
}
Other commands
- e002 SELECT INBOX selects the INBOX mail box.
- e003 SEARCH ALL retrieves all of the mail ID's. It's also possible to pass a query.
- e004 FETCH 1:10 (RFC822.SIZE ENVELOPE FLAGS) retrieves the size, envelope and flags of mail ID 1 through 10.
- e005 FETCH 1 BODY[TEXT] retrieves the body of mail ID 1.
- e006 CLOSE closes the selected mail box.
- e007 LOGOUT logs you off again.
Disconnect
writer.Close(); reader.Close(); stream.Close(); connection.Close();
10 March 2014
MVC - Resize an image
It turns out there is a helper class in the System.Web namespace for treating images. This class, WebImage, makes things like resizing e.g. a user profile picture when it is uploaded very easy:
public void FormatPicture(HttpPostedFileBase file) {
WebImage image = new WebImage(file.InputStream);
if (image.Width > 85 || image.Height > 85) {
int w = image.Width;
int h = image.Height;
if (w > h) {
w = 85;
h = image.Height * w / image.Width;
}
else {
h = 85;
w = image.Width * h / image.Height;
}
image.Resize(w, h);
}
image.Save(PictureFilepath);
}
The HttpPostedFileBase file comes from the view model and contains the uploaded image from the file control. In this example the image is resized to a maximum of 85 pixels, with the smaller dimension interpolated, and stored.
MVC - Download a file from an action
It is often usefull for files to be downloaded through code, and not directly. In the case of MVC, the download would go via an action. In the following example I download a users profile picture that way, while the actual file is not accessible directly:
[Authorize]
public ActionResult Picture() {
if (App.GetCurrentUser().HasPicture) { //Finds out if the current user has a picture in App_Data/UserPictures.
io.FileStream stream = io.File.OpenRead(App.GetCurrentUser().PictureFilepath);
byte[] data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
stream.Close();
stream.Dispose();
return File(data, MediaTypeNames.Application.Octet, App.GetCurrentUser() + ".jpg");
}
else {
return null;
}
}
The file is read into memory and the streamed back as an action result of type File. This principle also allows for files to be generated on the fly without them ever being stored on disc.
Subscribe to:
Posts (Atom)