Social Icons

Pages

Saturday, May 7, 2016

Apply group by clause on Datatable in Vb.net

       'create data table
        Dim dtExpensesType As New DataTable
        dtExpensesType.Columns.Add("country", GetType(String))
        dtExpensesType.Columns.Add("year", GetType(String))
        dtExpensesType.Columns.Add("act_expenses", GetType(Decimal))
        dtExpensesType.Columns.Add("Bud_expenses", GetType(Decimal))

        dtExpensesType.Rows.Add("Malaysia", "2013", 10000, 15000)
        dtExpensesType.Rows.Add("Malaysia", "2014", 30000, 32000)
        dtExpensesType.Rows.Add("Malaysia", "2015", 90000, 80000)
        dtExpensesType.Rows.Add("Malaysia", "2016", 10000, 11000)

        dtExpensesType.Rows.Add("Singapore", "2013", 20000, 25000)
        dtExpensesType.Rows.Add("Singapore", "2014", 30000, 42000)
        dtExpensesType.Rows.Add("Singapore", "2015", 90000, 30000)
        dtExpensesType.Rows.Add("Singapore", "2016", 20000, 81000)

        dtExpensesType.Rows.Add("China", "2013", 20000, 25000)
        dtExpensesType.Rows.Add("China", "2014", 30000, 42000)
        dtExpensesType.Rows.Add("China", "2015", 90000, 30000)
        dtExpensesType.Rows.Add("China", "2016", 20000, 81000)

                  'groub by country
                   Dim groupByQuery = From row In dtExpensesType.AsEnumerable()
                           Group row By Name = New With {
                        Key .country = row("country")
                        } Into NameGroup = Group
                           Select New With {
                        .country = Name.country,
                        .act_expenses = NameGroup.Sum(Function(r) r("act_expenses")),
                        .Bud_expenses = NameGroup.Sum(Function(r) r("Bud_expenses"))
                    }

                 Dim val
                For Each val In groupByQuery
                Console.WriteLine(val.country & "," & val.act_expenses & "," &  
                val.Bud_expenses)

                Next

Friday, May 6, 2016

Working with image in Vb.net (Re-size image, Crop image, Compress Image)

Re-size Image

Private Function ResizeImage(ByRef image As Image, _
                                        ByVal size As Size, _
                                        Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Integer
        Dim newHeight As Integer

        Try
            If preserveAspectRatio Then
                Dim originalWidth As Integer = image.Width
                Dim originalHeight As Integer = image.Height
                Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
                Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
                Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
                newWidth = CInt(originalWidth * percent)
                newHeight = CInt(originalHeight * percent)
            Else
                newWidth = size.Width
                newHeight = size.Height
            End If
            Dim newImage As Image = New Bitmap(newWidth, newHeight)
            Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
            End Using
            image = newImage
            Return newImage

        Catch ex As Exception
            Throw
        End Try
    End Function

Crop Image

Public Function CropBitmap(ByVal bitmap As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
        Try
            Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
            Dim cropped As Bitmap = bitmap.Clone(rect, bitmap.PixelFormat)
            Return cropped
        Catch ex As Exception
         Throw
        End Try

 End Function

Compress Image

Public Shared Function CompressImageTo16BMP(ByVal img As Image) As Bitmap
        Try
        Dim bmp As Bitmap = New Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555)
        Dim gr As Graphics = Graphics.FromImage(bmp)
        gr.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        Return bmp
        Catch ex As Exception
            Throw
        End Try
End Function

Monday, May 2, 2016

Create Two / Three Column Responsive Layout

Two column layout

HTML
<div class="divleft" style="background-color:green; height:200px"> </div>
<div class="divright" style="background-color:yellow; height:200px"> </div>
CSS 
.divleft {
  width: 50%;
  float: left;
}
.divright {
  width: 50%;
  float: right;
//make div width 100% 
@media screen and (max-width:900px){
 .divleft {
  width: 100%;
  }
   .divright {
  width: 100%;
  }
}

Three column layout

HTML
<div class= "divleft" style="background-color:green; height:200px"> </div>
<div class= "divmiddle" style="background-color:purple; height:200px"> </div>
<div class= "divright" style="background-color:yellow; height:200px"> </div>
CSS 

//Fixed width for left div
.divleft {
  width: 600px;
  float:left;
}

//width - 50%
.divmiddle {
    float:left;
  width: -webkit-calc((100% - 600px)/2);
}

.divright {
    float:left;
  width: -webkit-calc((100% - 600px)/2);
}

//Move right div down and make 100% width
@media screen and (max-width:1000px) {
  .divmiddle {
    width: -webkit-calc((100% - 600px));
  }
  .divright {
    width: 100%;
  }
}

//make 100% width
@media screen and (max-width:600px) {
    .divleft {
    width: 100%;
  }
  .divmiddle {
    width: 100%;
  }
  .divright {
    width: 100%;
  }
}