Wednesday, September 11, 2013

Stamping an image with text and Image

public static void AddWaterMark(MemoryStream ms, string watermarkText, MemoryStream outputStream)
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            Graphics gr = Graphics.FromImage(img);
            Font font = new Font("Tahoma", (float)40);
            Color color = Color.FromArgb(50, 241, 235, 105);
            double tangent = (double)img.Height / (double)img.Width;
            double angle = Math.Atan(tangent) * (180 / Math.PI);
            double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width)) / 2;
            double sin, cos, opp1, adj1, opp2, adj2;

            for (int i = 100; i > 0; i--)
            {
                font = new Font("Tahoma", i, FontStyle.Bold);
                SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue);

                sin = Math.Sin(angle * (Math.PI / 180));
                cos = Math.Cos(angle * (Math.PI / 180));
                opp1 = sin * sizef.Width;
                adj1 = cos * sizef.Height;
                opp2 = sin * sizef.Height;
                adj2 = cos * sizef.Width;

                if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width)
                    break;
                //
            }

            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            gr.SmoothingMode = SmoothingMode.AntiAlias;
            gr.RotateTransform((float)angle);
            gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat);

            img.Save(outputStream, ImageFormat.Jpeg);
        }


in the same place where you call gr.DrawString, you can also do gr.DrawImage(position, size, overlayImage). It helps if your image-to-overlay is loaded from a PNG file (with transparency) to produce the best quality.


One of the best way to stamp an image with time and location. 
Ref of the above example is from http://stackoverflow.com/questions/1224653/place-watermark-image-on-other-images-c-asp-net

Thursday, June 6, 2013

Strategy Pattern

To describe the strategy pattern, lets take an example of Football game.
The specific design problem is,
       when game is in progress any team can change its strategy from defend to attack or vice-          
       versa.

The solution for the same is strategy pattern.
         We need to let the algorithm vary independently from clients that uses it.
Very soon I will update article with sample code and class diagram.

Login failed for user 'IIS APPPOOL\ASP.NET v4.0'

To solve this problem we need to change the application pool's identity. 

You can change the ApplicationPoolIdentity from IIS7 -> Application Pools -> Advanced Settings.AdvancedSettings
Under ApplicationPoolIdentity you will find local system. This will make your application run under NT AUTHORITY\SYSTEM, which is an existing login for the database by default.

Monday, January 21, 2013

Devart oracle issue: ORA-01482

By default, strings in EF Code-First are considered as unicode and without explicit length. They would be VARCHAR2 if the [not unicode string] and [length < 4000 symbols] settings were specified in mapping for a particular property.

Solution for the attribute mapping. Specify the MaxLength(123) attribute if you have the VARCHAR2(123) column in your database. Also turn off PropertyMaxLengthConvention from the Conventions collection in DbModelBuilder (this convention is used for unicode strings).

Solution for the smooth mapping is just Set HasMaxLength(123) and IsUnicode(false) for the corresponding property. In this case there is no need to turn off PropertyMaxLengthConvention.

And we have to disable auto proxy generation if we are using EF Code-First approch. This can be done by writing
Configuration.ProxyCreationEnabled = false;
in the constructor of the Context class.


Ref: http://forums.devart.com