1. C# / Говнокод #7121

    +113

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    public string GetNormalImage(int newWidth, int newHeight, string sufix = "normal") {
                String[] tmp = _originalImagePath.Split('.');
                String newImagePath = "";
                for (int i = 0; i < tmp.Length - 1; i++)
                {
                    newImagePath += tmp[i];
                    newImagePath += "_";
                }
                newImagePath += sufix + ".";
                newImagePath += tmp[tmp.Length - 1];
    
                
                Image oldImage = Image.FromFile(_originalImagePath);
                if (oldImage.Height >= oldImage.Width) {
                    Image newImage;
                    newImage = FixedSize(oldImage, newWidth, newHeight);
                    newImage.Save(newImagePath);
                } else {
    
                    float heightRatio = (float)newHeight / (float)oldImage.Height;
                    float widthRatio = (float)newWidth / (float)oldImage.Width;
    
                    float bestRatio = 1;
                    if (heightRatio < widthRatio) {
                        bestRatio = heightRatio;
                    } else {
                        bestRatio = widthRatio;
                    }
    
                    var result = new System.Drawing.Bitmap((int)Math.Round(oldImage.Width * bestRatio), (int)Math.Round(oldImage.Height * bestRatio));
                    using (var graphics = Graphics.FromImage(result))
                    {
                        graphics.CompositingQuality = CompositingQuality.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.DrawImage(oldImage, new Rectangle(Point.Empty, new Size((int)Math.Round(oldImage.Width * bestRatio), (int)Math.Round(oldImage.Height * bestRatio))));
                    }
                    result.Save(newImagePath);
                }
    
                return newImagePath;
            }

    ресайз изображения

    tuxcod, 30 Июня 2011

    Комментарии (7)
  2. C# / Говнокод #7085

    +109

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    if (File.Exists(local)) {
                        if (Connect()) {
                            if (ftpConnection.Exists(remote)) {
    //code
                            }
                        }
                    }

    Irdis, 27 Июня 2011

    Комментарии (21)
  3. C# / Говнокод #7081

    +114

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    static Queue<string> getProxiesFromFile(string filename)
            {
                Queue<string> temp=new Queue<string>();
                System.IO.StreamReader file;
                string line;
                // Read the file and display it line by line.
                if (filename == null)
                {
                    file = new System.IO.StreamReader("proxy.txt");
                }
                else
                {
                    try
                    {
                        file = new System.IO.StreamReader(filename);
                    }
                    catch (FileNotFoundException)
                    {
                        throw;
                    }
                }
                while ((line = file.ReadLine()) != null)
                {
                    temp.Enqueue (line);
                    Console.WriteLine(line);
                }
                return temp;
            }

    Считываем файл построчно и заносим в Queue<string> .

    gg123, 27 Июня 2011

    Комментарии (6)
  4. C# / Говнокод #7061

    +126

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    public static IEnumerable<TItem> GetNextItemFrom<TItem>(IEnumerable<TItem> Collection)
            {
                foreach (var Item in Collection)
                    yield return Item;
            }

    Говногость, 26 Июня 2011

    Комментарии (10)
  5. C# / Говнокод #7036

    +116

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    if ( check < 50000 ) {
        primaryAnimation = primaryAnimation;
    } else {
        int ani = (check - 50000) / 100;
        primaryAnimation = _OptionalAnimations["Run"][ani];
     }

    Найдено в недрах загрузчика MD2-моделек для XNA.

    RaZeR, 23 Июня 2011

    Комментарии (9)
  6. C# / Говнокод #7035

    +115

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
    
        
        string cid = row.Cells[1].Text;
        bool status = chkStatus.Checked;
    
        
        string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;";
        string query = "UPDATE Categories SET Approved = @Approved WHERE CategoryID = @CategoryID";
            
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand(query, con);
    
        
        com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;
        com.Parameters.Add("@CategoryID", SqlDbType.Int).Value = cid;
    
        
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
    
        
        LoadData();
    }

    Полезный говнокод

    in4man, 23 Июня 2011

    Комментарии (24)
  7. C# / Говнокод #7023

    +119

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    // создаём источник для репитера
    
    private DataTable EventsDataTable
            {
                get
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add(
                        new DataColumn("ID", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("day", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("date", typeof(DateTime)));
                    dt.Columns.Add(
                        new DataColumn("title", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("url", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("description", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("location", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("place", typeof(string)));
                    dt.Columns.Add(
                        new DataColumn("FileDirRef", typeof(string)));
                    // Добавляем строчки
                    foreach (EventInfo ei in CalendarEvents)
                    {
                        DataRow dr = dt.NewRow();
                        dr["day"] = ei.EventDate.Date.ToString("ddMMyyyy");
                        dr["date"] = ei.EventDate;
                        dr["title"] = ei.Title;
                        dr["location"] = ei.Location;
                        dr["ID"] = ei.ID;
                        dr["FileDirRef"] = ei.FileDirRef;
                        dt.Rows.Add(dr);
                    }
                    return dt;
                }
            }
    
    
    protected void repeaterItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    if (e != null
                        && e.Item != null
                        && e.Item.DataItem != null
                        && e.Item.DataItem is DataRow)
                    {
                        DataRow dataItem = (DataRow)e.Item.DataItem;
    
                        Label date = (Label)(e.Item.FindControl("date"));
                        date.Text = 
                            dataItem["date"] != null
                            ? Convert.ToDateTime(dataItem["date"].ToString()).ToString()
                            : Convert.ToDateTime(dataItem["Created"].ToString()).ToString();
                        date.Text = date.Text.Substring(0, date.Text.Length - 3);
    
                        HyperLink title = (HyperLink)(e.Item.FindControl("title"));
                        title.Text = dataItem["title"].ToString();
                        Label location = (Label)(e.Item.FindControl("location"));
                        location.Text = "Расположение: " + dataItem["location"].ToString();
                    }
    }

    Современный способ привязки данных в asp.net Repeater

    Gnet, 22 Июня 2011

    Комментарии (41)
  8. C# / Говнокод #7016

    +120

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    var trimmedKey = Regex.Split(key, @"\.").Last();
                    if (_options.Any(o => o == ModelBinderOptions.ExpectUnderLineSymbolAsPrefixDelimiter))
                        trimmedKey = Regex.Split(trimmedKey, "_").Last();
    
                    if (_allRequiredParameters.Any(p => p.Key.ToLower() == trimmedKey.ToLower()))
                    {
                        var param = _allRequiredParameters.Single(p => p.Key.ToLower() == trimmedKey.ToLower());
    
                        try
                        {
                            if (param.Value != typeof(string))
                            {
                                if (Nullable.GetUnderlyingType(param.Value) != null)
                                {
                                    try
                                    {
                                        var parseMethod = Nullable.GetUnderlyingType(param.Value).GetMethods().Where(m => m.Name == "Parse").First(m => m.GetParameters().Count() == 1 && m.GetParameters().First().ParameterType == typeof(string));
                                        var value = parseMethod.Invoke(null, new object[] { form[key] });
                                        formValues.Add(param.Key, value);
                                    }
                                    catch(Exception)
                                    {
                                        formValues.Add(param.Key, null);
                                    }
                                }
                                else
                                {
                                    var parseMethod = param.Value.GetMethods().Where(m => m.Name == "Parse").First(m => m.GetParameters().Count() == 1 && m.GetParameters().First().ParameterType == typeof(string));
                                    var value = parseMethod.Invoke(null, new object[] { form[key] });
                                    formValues.Add(param.Key, value);
                                }
                                
                            }
                            else
                            {
                                formValues.Add(param.Key, form[key]);
                            }
                        }
                        catch (Exception)
                        {
                            // Если произошла ошибка парсинга - печально, но ничего не поделать
                        }
                    }

    Фееричный парсер

    dans, 21 Июня 2011

    Комментарии (20)
  9. C# / Говнокод #7004

    +130

    1. 1
    2. 2
    3. 3
    4. 4
    private static bool? GetBoolFromObject(object o)
            {
                return string.IsNullOrEmpty(o.ToString()) ? (bool?)null : (bool)o;
            }

    и как такое можно только писать...

    testguru, 20 Июня 2011

    Комментарии (39)
  10. C# / Говнокод #6999

    +132

    1. 1
    for(dynamic o=0; o!=237; o++){

    Ох, что мне досталось поддерживать... dynamic почти везде... :( Кто это написал??? Хочу этого человека взять и @#$%^&... :(

    Говногость, 19 Июня 2011

    Комментарии (26)