Respond needs javascript to run. To find out more click here
Programlama » Sayfa 2 » Serdar Demir
RSS
 

Archive for the ‘Programlama’ Category

Asp.net te Google URL Shortener API kullanımı

10 Ağu

Bir oyun için yapılan projede ürünlerin idlerini kendi hostuma kaydeyip ürünü alanlara bunun linkini veriyorum onlarda settingslerden ayarlıyorlar. Fakat burda soyle bir durum var oyun tarafında işlem yapabilemk için url ile bazı bilgiler taşımam gerekiyor bu da haliyle çok uzun bir link oluyor Google ın URL Shortener API sini kullanarak bu durumu düzeltebiliriz.

public class GoogleUrlShortnerApi
    {
        private const string key = "kendi apiniz";
//https://code.google.com/apis/console/ adresinden api keyinizi alabilirsiniz

        public static string Shorten(string url)
        {
            string post = "{\"longUrl\": \"" + url + "\"}";
            string shortUrl = url;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);

            try
            {
                request.ServicePoint.Expect100Continue = false;
                request.Method = "POST";
                request.ContentLength = post.Length;
                request.ContentType = "application/json";
                request.Headers.Add("Cache-Control", "no-cache");

                using (Stream requestStream = request.GetRequestStream())
                {
                    byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                    requestStream.Write(postBuffer, 0, postBuffer.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader responseReader = new StreamReader(responseStream))
                        {
                            string json = responseReader.ReadToEnd();
                            shortUrl = Regex.Match(json, @"""id"": ?""(?.+)""").Groups["id"].Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // if Google's URL Shortner is down...
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            return shortUrl;
string url = GoogleUrlShortnerApi.Shorten("http://adasddasdda.com/Content/Data/Keys/76a214c5-18ac-ace4-219d-278bbbd614c3.txt");

Sonuç:
Eski:http://adasddasdda.com/Content/Data/Keys/76a214c5-18ac-ace4-219d-278bbbd614c3.txt

Yeni: http://goo.gl/Wtnm9

 
No Comments

Posted in Asp.net

 

Generic handler da Session kullanımı

03 Ağu

Generic handlerlar genellikle jquery ve ajax isteklerinde kullandıgımız sayfalardır aslında aynı işlemleri bir aspx sayfasında yapabiliriz. Fakat generic handlerlar daha hızlıdır (aspx sayfa render edilcek life cycle var vs.) ve server ı yormazlar o yuzden generic handlerları kullanmanızı tavsiye ederim. Fakat her kontrole default olarak ulaşamıyorsunuz Örneğin bir sayfa da session a deger attınız bunu generic handler da kullanmak istiyorsunuz. Bunun için System.Web.SessionState.IRequiresSessionState interefacini implemente etmeniz gerekiyor.

public class Download : IHttpHandler, System.Web.SessionState.IRequiresSessionState

context.Session["Anahtar"].ToString();

bu şekilde sesion bilgisine ulaşabilirsiniz.

 

 

 
No Comments

Posted in Asp.net

 

Asp.net ile kaliteli resim küçültme

27 Tem

Bugün çalıştıgım şirkette kullandığım küçük resim oluşturma kodlarımı değiştirmem gerekti çünkü az da olsa resimleri küçültürken bozuyordu ama iş görüyordu.

protected void Page_Load(object sender, EventArgs e)
{
ResizeImage(@”C:\Users\Public\Pictures\Sample Pictures\3.jpg”, @”C:\Users\Public\Pictures\Sample Pictures\5.jpg”, 160, 160, ImageFormat.Jpeg);
}

public static void ResizeImage(string FileNameInput, string FileNameOutput, double ResizeHeight, double ResizeWidth, ImageFormat OutputFormat)
{
using (System.Drawing.Image photo = new Bitmap(FileNameInput))
{
double aspectRatio = (double)photo.Width / photo.Height;
double boxRatio = ResizeWidth / ResizeHeight;
double scaleFactor = 0;
if (photo.Width < ResizeWidth && photo.Height < ResizeHeight)
{
scaleFactor = 1.0;
}
else
{
if (boxRatio > aspectRatio)
scaleFactor = ResizeHeight / photo.Height;
else
scaleFactor = ResizeWidth / photo.Width;
}
int newWidth = (int)(photo.Width * scaleFactor);
int newHeight = (int)(photo.Height * scaleFactor);
using (Bitmap bmp = new Bitmap(newWidth, newHeight))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(photo, 0, 0, newWidth, newHeight);
if (ImageFormat.Png.Equals(OutputFormat))
{
bmp.Save(FileNameOutput, OutputFormat);
}
else if (ImageFormat.Jpeg.Equals(OutputFormat))
{
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
using (encoderParameters = new System.Drawing.Imaging.EncoderParameters(1))
{
encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90L);
bmp.Save(FileNameOutput, info[1], encoderParameters);
}
}
}
}
}
}

Sonuç:

 
1 Comment

Posted in Asp.net