Image Resize With CSharp

Often it is necessary to resize an image that has been uploaded. To do this you will need the following using classes:-\r\n\r\n

\r\nusing System.Drawing;\r\nusing System.Drawing.Imaging;\r\n

\r\n\r\nThen here are some examples on functions with the resize code:-\r\n\r\n

\r\npublic static string ResizeImageAndSave(int Width, int Height, string imageUrl,string destPath)\r\n	{\r\n        \r\n        \r\n        System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(imageUrl));\r\n\r\n        double widthRatio = (double)fullSizeImg.Width / (double)Width;\r\n        double heightRatio = (double)fullSizeImg.Height / (double)Height;\r\n        double ratio = Math.Max(widthRatio, heightRatio);\r\n        int newWidth = (int)(fullSizeImg.Width / ratio);\r\n        int newHeight = (int)(fullSizeImg.Height / ratio);\r\n        \r\n        System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);\r\n        System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight,dummyCallBack, IntPtr.Zero);\r\n        DateTime MyDate = DateTime.Now;\r\n        String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png";\r\n        thumbNailImg.Save(HttpContext.Current.Server.MapPath(destPath) + MyString, ImageFormat.Png);\r\n        thumbNailImg.Dispose();\r\n        return MyString;\r\n	}\r\n\r\n    public static bool ThumbnailCallback()\r\n    {\r\n        return false;\r\n    }\r\n

\r\n

Posted in Uncategorized

File.Delete Command

Here is an example on how to delete files in the file sytem:-\r\n\r\n

\r\npublic static void DeleteFile(string FilePath)\r\n    {\r\n        try\r\n        {\r\n            FileInfo TheFile = new FileInfo(HttpContext.Current.Server.MapPath(FilePath));\r\n            if (TheFile.Exists)\r\n            {\r\n                File.Delete(HttpContext.Current.Server.MapPath(FilePath));\r\n            }\r\n            else\r\n            {\r\n                throw new FileNotFoundException();\r\n            }\r\n        }\r\n        catch (FileNotFoundException e)\r\n        {\r\n            LogEntry log = new LogEntry();\r\n            log.EventId = 701;\r\n            log.Message = "Error Deleting File\\r\\n" + FilePath + "\\r\\n" + e.ToString() + "\\r\\n" + e.StackTrace;\r\n            log.Categories.Add("General");\r\n            log.Severity = TraceEventType.Information;\r\n            log.Priority = 5;\r\n            Logger.Write(log);\r\n        }\r\n        catch (Exception e)\r\n        {\r\n            LogEntry log = new LogEntry();\r\n            log.EventId = 1301;\r\n            log.Message = "Error Deleting File Exception\\r\\n" + FilePath + "\\r\\n" + e.ToString() + "\\r\\n" + e.StackTrace;\r\n            log.Categories.Add("General");\r\n            log.Severity = TraceEventType.Information;\r\n            log.Priority = 5;\r\n            Logger.Write(log);\r\n        }\r\n\r\n    }\r\n

\r\n\r\nThe example code above has some custom error exception handling that you may want to remove.

Posted in Uncategorized
Tags:

Writing Dynamically To The Page

I was making a yearly calendar and i needed to output the cells to represent the days in the year. At first i just made a public variable, filled it up with html and then outputed it to the page like this:-

\r\n

\r\n<%=variablename%>\r\n

\r\nThis seemed to work well but the page didn”t update on postback methods because the html was being rendered before any methods were executing.\r\nI figured there must be a way to output to the current page so i had a look around and found some code that updated a panel control. To send it raw html use this command:-\r\n

\r\npanMainCalendar.Controls.Add(new LiteralControl("


"));\r\n

\r\n\r\nTo add a control try this:-\r\n

\r\nButton NLab = new Button();\r\nUnit NU = new Unit(100,UnitType.Percentage);\r\nNLab.Width = NU;\r\nNLab.Height = NU;\r\nNLab.Click += Day_Click;\r\nNLab.CssClass = "ButtonCell";\r\nNLab.ID = "lbCell_"+Day + "_" + Month + "_" + Year;\r\npanMainCalendar.Controls.Add(NLab);\r\n

\r\n\r\nGood Luck\r\n

Posted in Uncategorized
Tags:

Accessing Post Variables in C# ASP.Net

I was writing dynamic form variables to the page and i needed to retrieve their values when updated. When i tried to access the variables with request.params[] collection i noticed that the keys were what id i had given them with a string appended to the start. I wrote this function to easiliy access the variables:-\r\n\r\n

\r\npublic static string PostVal(string key)\r\n    {\r\n        string retval = "";\r\n        foreach (string s in HttpContext.Current.Request.Form.AllKeys)\r\n        {\r\n            string[] sr = s.Split(''$'');\r\n            if (sr[sr.Length - 1] == key)\r\n            {\r\n                retval = HttpContext.Current.Request.Form[s];\r\n            }\r\n        }\r\n        return retval;\r\n    }\r\n
Posted in Uncategorized
Tags:

Colour Picker

I needed to have custom colours in my project so i looked at a few controls out there and the one i chose is from http://www.obout.com/ It seemed to do the job quite nicely.’,

Posted in Uncategorized
Tags:

Calculating Date Intervals From Days Of Week

I have to calculate date intervals between two global dates depending on the inclusion of certain days of the week, such as monday to wednesday. All you need is two calendars and check boxes for the days of the week to include. This is the calculation for working out the dates:-\r\n

\r\nDateRangeOutput = "";\r\n        string DateFrom = Date1.Text;\r\n        string[] TmpArr = DateFrom.Split(''/'');\r\n        int DayFrom = Convert.ToInt32(TmpArr[0]);\r\n        int MonthFrom = Convert.ToInt32(TmpArr[1]);\r\n        int YearFrom = Convert.ToInt32(TmpArr[2]);\r\n        DateTime MasterFrom = new DateTime(YearFrom, MonthFrom, DayFrom);\r\n        \r\n        string DateTo = Date2.Text;\r\n        TmpArr = DateTo.Split(''/'');\r\n        int DayTo = Convert.ToInt32(TmpArr[0]);\r\n        int MonthTo = Convert.ToInt32(TmpArr[1]);\r\n        int YearTo = Convert.ToInt32(TmpArr[2]);\r\n        DateTime MasterTo = new DateTime(YearTo, MonthTo, DayTo);\r\n\r\n        //DayOfWeek TDay=MasterFrom.DayOfWeek;\r\n        int CDay = Convert.ToInt32(MasterFrom.DayOfWeek);\r\n        bool AddTo=IsDayChecked(CDay);\r\n        DateTime StartDate = new DateTime();\r\n        DateTime EndDate = new DateTime();\r\n        bool StartDateCont = true;\r\n        bool EndDateCont = true;\r\n        if(AddTo){\r\n		    StartDate=MasterFrom;\r\n		    EndDate=MasterFrom;\r\n	    }else{\r\n		    StartDateCont=false;\r\n            EndDateCont = false;\r\n	    }\r\n\r\n        DateTime PointerDate=new DateTime(YearFrom,MonthFrom,DayFrom+1);\r\n	    int DayCount=1;\r\n	    while(PointerDate<=MasterTo){\r\n		    CDay=Convert.ToInt32(PointerDate.DayOfWeek);\r\n            AddTo = IsDayChecked(CDay);\r\n		    if(AddTo){\r\n                if (!StartDateCont)\r\n                {\r\n                    StartDate = PointerDate;\r\n                    StartDateCont = true;\r\n                }\r\n			    EndDate=PointerDate;\r\n		    }else{\r\n                if (StartDateCont) AddDates(StartDate.ToString("dd-MM-yyyy"), EndDate.ToString("dd-MM-yyyy"));\r\n			    StartDateCont=false;\r\n		    }\r\n		    DayCount++;\r\n		    //PointerDate=new DateTime(YearFrom,MonthFrom,DayFrom+DayCount);\r\n            PointerDate=PointerDate.AddDays(1);\r\n		    \r\n	    }\r\n	    if(StartDateCont){\r\n            EndDate = new DateTime(YearFrom, MonthFrom , DayFrom  ).AddDays(DayCount - 1);\r\n		    //alert(DayCount);\r\n            AddDates(StartDate.ToString("dd-MM-yyyy"), EndDate.ToString("dd-MM-yyyy"));\r\n	    }\r\n\r\n

\r\n\r\nThe following code is what i do with the dates:-\r\n

\r\nprivate void AddDates(string DateRFrom,string DateRTo){\r\n        panAddSeason.Controls.Add(new LiteralControl(ReturnDateText(DateRFrom, DateRTo)));\r\n        \r\n        DateCount++;\r\n    }\r\n\r\n    private string ReturnDateText(string DateRFrom,string DateRTo){\r\n        string DateText="

";\r\n DateText+=" to (dd-mm-yyyy)
";\r\n return DateText;\r\n }\r\n

\r\n\r\nThe following function determines what days to include in the calculation:-\r\n

\r\nprivate bool IsDayChecked(int Day)\r\n    {\r\n        bool retval = false;\r\n        switch (Day)\r\n        {\r\n            case 0:\r\n                retval = chkDayIncludedSun.Checked;\r\n                break;\r\n            case 1:\r\n                retval = chkDayIncludedMon.Checked;\r\n                break;\r\n            case 2:\r\n                retval = chkDayIncludedTues.Checked;\r\n                break;\r\n            case 3:\r\n                retval = chkDayIncludedWed.Checked;\r\n                break;\r\n            case 4:\r\n                retval = chkDayIncludedThur.Checked;\r\n                break;\r\n            case 5:\r\n                retval = chkDayIncludedFri.Checked;\r\n                break;\r\n            case 6:\r\n                retval = chkDayIncludedSat.Checked;\r\n                break;\r\n        }\r\n        return retval;\r\n    }\r\n

\r\n\r\nThis calculation is especially useful in calculating seasons for accommodation booking engines.’,

Posted in Uncategorized
Tags:

Microsoft AJAX Extentions and Toolkit Ramblings

We wanted to upgrade the usability of the new site we are developing, i have briefly seen info about the microsoft ajax extentions so i thought i would check it out. The main site is http://ajax.asp.net which has tutorials, articles and downloads. I downloaded the extensions for asp.net 2.0 and i also downloaded the toolkit. Now i had the code i thought i would upgrade one of my pages, i added the script manager and an update pane to one of my pages, in the script manager i set the property “Enable Partial Rendering” to true and then pressed play. The page loaded and when i clicked the submit button it submitted the form and updated the message label without reloading the page. Cool i thought this is going to be straight forward so i thought now i will check out the AJAX Toolkit. I unzipped the solution, loaded it into visual studio and pressed play. Well the build failed because the System.EnterpriseServices assembly wouldn”t load because it didn”t have the correct permissions. So off to google i went, after many unhelpful sites i figured it has something to do with the security levels in the web.config. So i updated the directive “trust” and set it to\r\n\r\n<trust level=”Full”/>\r\n\r\nI am not too experienced with windows permissions but i don”t think this is too unsafe. Anyway this got it working and i loaded the sample website. There seemed to be many cool controls and i worked out how i could use some of them in the current project. I added the tabs control without much drama but the autocomplete control is giving me trouble. What is cool about it is that it uses web services which i hadn”t done is asp.net before. They seem pretty straight forward which is great as we are going to be using them in the future. One thing i noticed about them is they don”t share the session from the ajax page accessing them.

Posted in Uncategorized
Tags:

Welcome to Developing ASP.

Welcome to Developing ASP.
I have been developing dot net for about a year now, with many years experience with PHP/MySQL and for the last 3 months have been using ASP.Net. I like to give back to the community as most of my learning comes from what i can find on the web. So here we are with a site dedicated to learning ASP.Net. You can find some of previous work that i have done in PHP at http://www.developing-php.com. There is some javascript stuff there to.

Posted in Uncategorized

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Posted in Uncategorized