var fonts = new PrivateFontCollection(); fonts.AddFontFile(Server.MapPath("~/Alix2.ttf")); using(var bm = new Bitmap(300, 100)) { using(var font = new Font(fonts.Families.First(), 22)) { using(var g = Graphics.FromImage(bm)) { g.DrawString("Hello fonts", font, Brushes.White, 50, 20); } } bm.Save(Server.MapPath("img.jpg"), ImageFormat.Jpeg); }
Показ дописів із міткою C#. Показати всі дописи
Показ дописів із міткою C#. Показати всі дописи
четвер, 15 вересня 2011 р.
C#: Draw string with custom font
вівторок, 30 серпня 2011 р.
C#, read sheets names from Excel (*.xls) file
var file = Server.MapPath("file.xls");
var cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", file);
using(var conn = new OleDbConnection(cs))
{
conn.Open();
var table = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var sheets = table.Rows.Cast().Select(x => x["TABLE_NAME"]).ToList();
grid.DataSource = sheets;
grid.DataBind();
}
понеділок, 4 липня 2011 р.
C#, read data from Excel (*.xls) file
var file = Server.MapPath("file.xls");
var cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", file);
using(var conn = new OleDbConnection(cs))
{
using(var cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn))
{
using(var adapter = new OleDbDataAdapter(cmd))
{
var table = new DataTable();
adapter.Fill(table);
grid.DataSource = table;
grid.DataBind();
}
}
}
вівторок, 22 березня 2011 р.
CultureInfo
en-GB - специфическая
ro - нейтральная
ro - нейтральная
var c = "ro";
//создаем специфическую с нейтральной
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(c);
//можно использовать нейтральную, например, Strings.ro.resx
Thread.CurrentThread.CurrentUICulture = new CultureInfo(c);
вівторок, 11 січня 2011 р.
C#, Get Current Path
public static class PathHelper
{
public static string GetCurrentPath()
{
string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
return System.IO.Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
}
public static string CombineWithCurrent(string path)
{
return System.IO.Path.Combine(GetCurrentPath(), path);
}
}
середа, 17 листопада 2010 р.
.NET Round
Math.Round(3.5);//=4
Math.Round(4.5);//=4
Math.Round(3.5, MidpointRounding.AwayFromZero);//=4
Math.Round(4.5, MidpointRounding.AwayFromZero);//=5
понеділок, 1 листопада 2010 р.
Кириллица в куках
Можно использовать UrlEncode, UrlDecode:
var cookie = new HttpCookie("user", Server.UrlEncode(name));
...
var name = Server.UrlDecode(cookie.Value);
четвер, 9 вересня 2010 р.
Remove html-tags from string
var value = Regex.Replace(value, "<[^>]*?>", string.Empty, RegexOptions.IgnoreCase);
пʼятниця, 27 серпня 2010 р.
Метод для провірки телефона
public static bool IsPhone(this string phone)
{
return Regex.IsMatch(phone, @"^\+?[0-9\-() ]{5,19}$");
}
четвер, 18 лютого 2010 р.
C#: replace XML special characters
System.Security.SecurityElement.Escape(string str);
& - &
< - <
> - >
" - "
' - '
Підписатися на:
Дописи (Atom)