February 2004 - Posts

Cropping image

So i was bizzy yesterday night till about 3:00 am with a problem of how the h*ll do i crop a image and after a lot of trying i gave up but found a nice function in some code example by i don't know who but it didn't work great i got a “A generic error occurred in GDI+. “ when i called the function and tryed to save the image.
The counter against this i found out very fast is:
System.Drawing.Image imageRigth = Crop(image,200,200,AnchorPosition.Center);
using(Bitmap bitmap = new Bitmap(imageRigth))
{
 bitmap.Save(MapPath(fileName), imageRigth.RawFormat);
}

So now i can finaly crop the pictures i upload :)

The function i found:
enum AnchorPosition
  {
   Top,
   Center,
   Bottom,
   Left,
   Right
  }

  static System.Drawing.Image Crop(System.Drawing.Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
  {
   int sourceWidth = imgPhoto.Width;
   int sourceHeight = imgPhoto.Height;
   int sourceX = 0;
   int sourceY = 0;
   int destX = 0;
   int destY = 0;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;

   nPercentW = ((float)Width/(float)sourceWidth);
   nPercentH = ((float)Height/(float)sourceHeight);

   if(nPercentH < nPercentW)
   {
    nPercent = nPercentW;
    switch(Anchor)
    {
     case AnchorPosition.Top:
      destY = 0;
      break;
     case AnchorPosition.Bottom:
      destY = (int)(Height - (sourceHeight * nPercent));
      break;
     default:
      destY = (int)((Height - (sourceHeight * nPercent))/2);
      break;
    }    
   }
   else
   {
    nPercent = nPercentH;
    switch(Anchor)
    {
     case AnchorPosition.Left:
      destX = 0;
      break;
     case AnchorPosition.Right:
      destX = (int)(Width - (sourceWidth * nPercent));
      break;
     default:
      destX = (int)((Width - (sourceWidth * nPercent))/2);
      break;
    }   
   }

   int destWidth  = (int)(sourceWidth * nPercent);
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap bmPhoto = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

   Graphics grPhoto = Graphics.FromImage(bmPhoto);
   grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

   grPhoto.DrawImage(imgPhoto,
    new Rectangle(destX,destY,destWidth,destHeight),
    new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
    GraphicsUnit.Pixel);

   grPhoto.Dispose();
   return bmPhoto;
  }

Give me more languages

Muhaha my first info post.

Ok so I wanted a multiple language page and that in a fast way so I’m using a “Assembly Resource File”.
Just make a default something named “language.resx” put your info in it then make a copy of it and name it “language.nl.resx” to make a Dutch language file.

Now make a Web Form and add the following code to the Page_Load
//Load the language file
ResourceManager ResourceManager1 = new ResourceManager("buur.language", System.Reflection.Assembly.GetExecutingAssembly());
//Set dutch a the default language
System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo("nl");
//
//or get the language that the users computer uses or if that file dus not exist use the default file(“language.resx”)
//System.Globalization.CultureInfo Culture = //System.Threading.Thread.CurrentThread.CurrentUICulture;
//
//Get the value A Key and get it from the Culture filled in
Label1.Text = ResourceManager1.GetString("A Key", Culture);

This is pritty easy but I want it in a dataset because the resx files are dataset’s but I don’t have a idea on how to do that any idea’s ?

Hello World?? Weblog?? Very One??

Sorry I don't really know what the best topic is for a first post.

Ok so here's my first shot: I'm warnar, I'm 17 years old a great newbie in the .NET world ( Thanks neighbor for getting me started ;D ).
I have been working/playing with programming/scripting for about 3 years now mostly PHP but also some Pascal and C++ (at school) but now my new challenge is ASP.NET and I have to get the hang of it in about 6 month because it is for a school project and yet again my neighbor helps me go for the .NET framework (Welcome jungle of new science).

I hope some of you can enjoy my learning in my blogs.

Have fun, Warnar