2019-04-02 13:43:30 +02:00
using System ;
using System.Drawing ;
using System.Drawing.Imaging ;
using System.IO ;
2019-05-14 13:37:20 +02:00
using System.Linq ;
2019-04-02 13:43:30 +02:00
using System.Net ;
2019-05-14 13:37:20 +02:00
using System.Reflection ;
using System.Runtime.InteropServices ;
using System.Windows ;
using System.Windows.Interop ;
2019-04-02 13:43:30 +02:00
using System.Windows.Media ;
using System.Windows.Media.Imaging ;
2019-05-14 13:37:20 +02:00
using MessageBox = System . Windows . MessageBox ;
using PixelFormat = System . Drawing . Imaging . PixelFormat ;
2019-04-02 13:43:30 +02:00
namespace ChatController.Utilities
{
public class Utils
{
2019-05-14 13:37:20 +02:00
public static DateTime DefaultDate = new DateTime ( 1 , 1 , 1 ) ;
2019-04-04 13:00:10 +02:00
2019-05-14 13:37:20 +02:00
public static ImageSource AvatarToImageSourceConverter ( string pAvatarPath , bool pIsEmployee , bool pIsGroup )
2019-04-02 13:43:30 +02:00
{
2019-05-14 13:37:20 +02:00
return CreateImageSourceFromPath ( pAvatarPath , pIsGroup , pIsEmployee ) ;
2019-04-02 13:43:30 +02:00
}
public static ImageSource CreateChatGroupPictureImageSource ( string pPicturePath , bool pIsGroup , bool pIsEmployee )
{
return CreateImageSourceFromPath ( pPicturePath , pIsGroup , pIsEmployee ) ;
}
private static ImageSource GetDefaultImageSource ( bool pIsGroup , bool pIsEmployee )
{
var defaultImage = Constants . EmployeeDefaultImagePath ;
if ( pIsGroup )
{
defaultImage = pIsEmployee ? Constants . TeamDefaultImagePath : Constants . NormalGroupDefaultImagePath ;
}
else if ( ! pIsEmployee )
{
defaultImage = Constants . CustomerDefaultImagePath ;
}
var resultBitmapImage = new BitmapImage ( ) ;
resultBitmapImage . BeginInit ( ) ;
resultBitmapImage . UriSource = new Uri ( defaultImage ) ;
resultBitmapImage . EndInit ( ) ;
return resultBitmapImage ;
}
public static ImageSource CreateImageSourceFromPath ( string pPathToImageFile , bool pIsGroup = false , bool pIsEmployee = false )
{
if ( ! string . IsNullOrEmpty ( pPathToImageFile ) )
{
Bitmap bitmap ;
var request = WebRequest . Create ( pPathToImageFile ) ;
using ( var response = request . GetResponse ( ) )
{
using ( var stream = response . GetResponseStream ( ) )
{
if ( stream ! = null )
{
bitmap = new Bitmap ( stream ) ;
}
else
{
return GetDefaultImageSource ( pIsGroup , pIsEmployee ) ;
}
}
}
using ( var memoryStream = new MemoryStream ( ) )
{
bitmap . Save ( memoryStream , ImageFormat . Png ) ;
memoryStream . Position = 0 ;
var bitmapImage = new BitmapImage ( ) ;
bitmapImage . BeginInit ( ) ;
bitmapImage . CacheOption = BitmapCacheOption . OnLoad ;
bitmapImage . StreamSource = memoryStream ;
bitmapImage . EndInit ( ) ;
memoryStream . Dispose ( ) ;
memoryStream . Close ( ) ;
return bitmapImage ;
}
}
return GetDefaultImageSource ( pIsGroup , pIsEmployee ) ;
}
public static ImageSource CreateImageSourceFromSmallerImage ( string pUri )
{
Bitmap bitmap ;
var request = WebRequest . Create ( pUri ) ;
using ( var response = request . GetResponse ( ) )
{
using ( var stream = response . GetResponseStream ( ) )
{
if ( stream = = null )
{
return null ;
}
bitmap = new Bitmap ( stream ) ;
}
}
using ( var memoryStream = new MemoryStream ( ) )
{
bitmap . Save ( memoryStream , ImageFormat . Png ) ;
memoryStream . Position = 0 ;
var bitmapImage = new BitmapImage ( ) ;
bitmapImage . BeginInit ( ) ;
bitmapImage . CacheOption = BitmapCacheOption . OnLoad ;
bitmapImage . StreamSource = memoryStream ;
bitmapImage . EndInit ( ) ;
memoryStream . Dispose ( ) ;
memoryStream . Close ( ) ;
return bitmapImage ;
}
}
public static string ReadStream ( WebResponse response )
{
var dataStream = response . GetResponseStream ( ) ;
if ( dataStream = = null )
{
return null ;
}
var reader = new StreamReader ( dataStream ) ;
var responseFromServer = reader . ReadToEnd ( ) ;
dataStream . Dispose ( ) ;
reader . Dispose ( ) ;
dataStream . Close ( ) ;
reader . Close ( ) ;
return responseFromServer ;
}
2019-05-14 13:37:20 +02:00
public static string GetAndCreateUserAppDataPath ( )
{
try
{
var localAppData = Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ;
var companyFilePath = Path . Combine ( localAppData , Resource . CompanyName ) ;
if ( ! Directory . Exists ( companyFilePath ) )
{
Directory . CreateDirectory ( companyFilePath ) ;
}
var bewoFilePath = Path . Combine ( companyFilePath , Resource . ApplicationFolderName ) ;
if ( ! Directory . Exists ( bewoFilePath ) )
{
Directory . CreateDirectory ( bewoFilePath ) ;
}
return bewoFilePath ;
}
catch ( Exception exception )
{
MessageBox . Show ( "Fehler beim Anlegen des Anwendungsordners. Sie besitzen nicht die erforderlichen Rechte, bitte wenden Sie sich an Ihren Systemadministrator.\n" + exception . Message , "BeWoPlaner" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
}
return Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
}
public static string GenerateTempName ( )
{
const int length = 6 ;
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
var random = new Random ( ) ;
var tempName = new string ( Enumerable . Repeat ( chars , length ) . Select ( s = > s [ random . Next ( s . Length ) ] ) . ToArray ( ) ) ;
return tempName ;
}
public static byte [ ] ImageToByteArray ( Image pImage )
{
try
{
using ( var memoryStream = new MemoryStream ( ) )
{
pImage . Save ( memoryStream , ImageFormat . Bmp ) ;
return memoryStream . ToArray ( ) ;
}
}
catch ( Exception e )
{
MessageBox . Show ( "Fehler: " + e . Message , "Fehler" , MessageBoxButton . OK ) ;
return null ;
}
}
public static ImageSource GetImageSourceFromIcon ( Icon pIcon )
{
var bitmap = new Bitmap ( pIcon . Width , pIcon . Height ) ;
var graphics = Graphics . FromImage ( bitmap ) ;
graphics . DrawIcon ( pIcon , 0 , 0 ) ;
graphics . Dispose ( ) ;
bitmap . Save ( "icon.ico" , ImageFormat . Icon ) ;
var imageSource = ImageSourceForBitmap ( bitmap ) ;
bitmap . Dispose ( ) ;
return imageSource ;
}
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject ( [ In ] IntPtr hObject ) ;
public static ImageSource ImageSourceForBitmap ( Bitmap bmp )
{
var handle = bmp . GetHbitmap ( ) ;
try
{
return Imaging . CreateBitmapSourceFromHBitmap ( handle , IntPtr . Zero , Int32Rect . Empty , BitmapSizeOptions . FromEmptyOptions ( ) ) ;
}
finally
{
DeleteObject ( handle ) ;
}
}
public static Icon ImageSourceToIcon ( ImageSource pImageSource )
{
var bitmapSource = ( BitmapSource ) pImageSource ;
var width = bitmapSource . PixelWidth ;
var height = bitmapSource . PixelHeight ;
var newWidth = width ;
var newHeight = height ;
var x = 0 ;
var y = 0 ;
if ( width < height )
{
newHeight = width ;
y = ( height - width ) / 2 ;
}
else
{
newWidth = height ;
x = ( width - height ) / 2 ;
}
var stride = width * ( ( bitmapSource . Format . BitsPerPixel + 7 ) / 8 ) ;
var memoryBlockPointer = Marshal . AllocHGlobal ( height * stride ) ;
bitmapSource . CopyPixels ( new Int32Rect ( x , y , newWidth , newHeight ) , memoryBlockPointer , newHeight * stride , stride ) ;
var bitmap = new Bitmap ( newWidth , newHeight , stride , PixelFormat . Format32bppPArgb , memoryBlockPointer ) ;
return Icon . FromHandle ( bitmap . GetHicon ( ) ) ;
}
public static DateTime FromUnixTimeStamp ( long pUnixTimeStamp )
{
return new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) . AddSeconds ( pUnixTimeStamp ) ;
}
public static byte [ ] ReadFully ( Stream pStream )
{
using ( var memStream = new MemoryStream ( ) )
{
pStream . CopyTo ( memStream ) ;
return memStream . ToArray ( ) ;
}
}
public static RotateFlipType OrientationToFlipType ( string orientation )
{
switch ( int . Parse ( orientation ) )
{
case 1 :
return RotateFlipType . RotateNoneFlipNone ;
case 2 :
return RotateFlipType . RotateNoneFlipX ;
case 3 :
return RotateFlipType . Rotate180FlipNone ;
case 4 :
return RotateFlipType . Rotate180FlipX ;
case 5 :
return RotateFlipType . Rotate90FlipX ;
case 6 :
return RotateFlipType . Rotate90FlipNone ;
case 7 :
return RotateFlipType . Rotate270FlipX ;
case 8 :
return RotateFlipType . Rotate270FlipNone ;
default :
return RotateFlipType . RotateNoneFlipNone ;
}
}
2019-04-02 13:43:30 +02:00
}
}