google

Newest Post

Showing posts with label custom. Show all posts
Showing posts with label custom. Show all posts

Convert base64 into Image, Resize the image and again convert it into base64

| Saturday, March 5, 2016
Read more »
Convert base64 into Image, Resize the image and again convert it into base64

Use Following Code:

                       byte[] bytes = (byte[])row["Picture"];

                        Image image;

                       //Convert byte[] into image
                        using (MemoryStream ms = new MemoryStream(bytes))
                        {
                            image = Image.FromStream(ms);
                        }

                        // Resize the image
                        Bitmap b = new Bitmap(200, 200);
                        Graphics g = Graphics.FromImage((Image)b);
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                        g.DrawImage(image, 0, 0, 200, 200);
                        g.Dispose();
                        image = (Image)b;

                        using (MemoryStream ms = new MemoryStream())
                        {
                            // Convert Image to byte[]
                            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            byte[] imageBytes = ms.ToArray();

                            // Convert byte[] to Base64 String
                            string base64String = Convert.ToBase64String(imageBytes);
                            obj.StudentPicture = base64String;
                        }

Convert base64 into Image, Resize the image and again convert it into base64

Posted by : Stranger
Date :Saturday, March 5, 2016
With 0comments

Custom Date Time Picker for Android

| Friday, November 6, 2015
Read more »
Android itself does not provide any date or time picker which can let user choose date and time together.

We can choose dates using DatePickerDialog and times using TimePickerDialog, which requires 2 different dialogs and 2 different actions. Instead of that we can use following code to provide selection of Date and Time in one dialog.

Following is the code of the layout file "date_time_picker.xml" which will be inflated to be displayed in the Date Time Picker Dialog.


 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:padding="10dp"  
   android:layout_height="match_parent">  
   <DatePicker  
     android:id="@+id/datepicker"  
     android:layout_width="match_parent"  
     android:calendarViewShown="true"  
     android:spinnersShown="false"  
     android:layout_weight="4"  
     android:layout_height="0dp" />  
   <TimePicker  
     android:id="@+id/timepicker"  
     android:layout_weight="4"  
     android:layout_width="match_parent"  
     android:layout_height="0dp" />  
   <Button  
     android:id="@+id/datetimeset"  
     android:layout_weight="1"  
     android:layout_width="match_parent"  
     android:text="Set"  
     android:layout_height="0dp" />  
 </LinearLayout>  

Following is the code of Button click in Java file which will open the dialog.


  public void SelectDateTime(View v) {  
     final View dialogView = View.inflate(this, R.layout.date_time_picker, null);  
     final AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
     dialogView.findViewById(R.id.datetimeset).setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datepicker);  
         TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.timepicker);  
         Calendar calendar = new GregorianCalendar(datePicker.getYear(),  
             datePicker.getMonth(),  
             datePicker.getDayOfMonth(),  
             timePicker.getCurrentHour(),  
             timePicker.getCurrentMinute());  
         SimpleDateFormat mSDF = new SimpleDateFormat("HH:mm:ss");  
         String time = mSDF.format(calendar.getTime());  
         int day = datePicker.getDayOfMonth();  
         int month = datePicker.getMonth();  
         int year = datePicker.getYear();  
         SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");  
         String formatedDate = sdf.format(new Date(year-1900, month, day));  
         editText_datetime.setText(formatedDate + ' ' + time);  
         alertDialog.dismiss();  
       }  
     });  
     alertDialog.setView(dialogView);  
     alertDialog.show();  
   }  


"SelectDateTime" method is called on button click which will fetch the values from Date Time picker into the EditText.

"SimpleDateFormat" class has been used to set the values in desired date and time formats.

Important

String formatedDate = sdf.format(new Date(year-1900, month, day));  

Always +/- 1900 with year formatted using SimpleDateFormat. 

Custom Date Time Picker
Prev
▲Top▲