Images working

This commit is contained in:
Matthew Burke 2023-07-27 18:59:51 -04:00
parent 1b65e9bb83
commit 09d86b86c2
7 changed files with 300 additions and 12 deletions

View File

@ -94,9 +94,10 @@
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="77*"/>
</Grid.ColumnDefinitions>
<Button Content="Add Image" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="20"/>
<Button Content="Remove" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="20"/>
<Button x:Name="AddImageButton" Content="Add Image" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="20" Click="Button_Click_2"/>
<Button Content="Remove" Grid.Column="1" Margin="0,0,5,1" HorizontalAlignment="Right" Width="75" Click="Button_Click_3"/>
</Grid>
<ListBox x:Name="ImageList" HorizontalAlignment="Left" Height="234" Grid.Row="1" VerticalAlignment="Top" Width="447"/>
</Grid>
</GroupBox>
</Grid>

View File

@ -12,6 +12,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
/**
* TODO:
@ -39,6 +41,8 @@ namespace PWAPPv2
Source.Patient patient;
List<Source.DataObjects.PWImage> images;
public MainWindow()
{
try
@ -48,6 +52,8 @@ namespace PWAPPv2
catch(Exception)
{ }
images = new List<Source.DataObjects.PWImage>();
apiconfig.LoadConfig("./Config/Config.xml");
DataConfig = new Source.Database.DatabaseConfig("./Config/Config.xml");
Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(DataConfig);
@ -130,23 +136,51 @@ namespace PWAPPv2
}
}
//Cancel button
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
//OK Button
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Source.DataObjects.Referral referral = new Source.DataObjects.Referral(apiCreds, patient,
Source.DataObjects.Referral referral;
if(images.Count == 0)
{
referral = new Source.DataObjects.Referral(apiCreds, patient,
(Source.DataObjects.ReferralTypeBox)TypeBox,
(Source.DataObjects.ReferToBox)ToBox,
(Source.DataObjects.ReferFromBox)FromBox,
fieldRemakrs, contact);
fieldRemakrs, contact, false);
}
else
{
referral = new Source.DataObjects.Referral(apiCreds, patient,
(Source.DataObjects.ReferralTypeBox)TypeBox,
(Source.DataObjects.ReferToBox)ToBox,
(Source.DataObjects.ReferFromBox)FromBox,
fieldRemakrs, contact, true);
}
try
{
string referralString = referral.ToJsonString();
string result = apiConnection.SendPostRequestAsync("api/PWMakeReferral", referralString);
if(images.Count > 0)
{
foreach(Source.DataObjects.PWImage im in images)
{
Source.DataObjects.Attachment att = new Source.DataObjects.Attachment(apiCreds, im, result);
string json = att.ToJsonString();
apiConnection.SendPostRequestAsync("api/PWAttachment", json);
}
MessageBox.Show("Referral added successfully!");
}
else
{
MessageBox.Show(result);
}
this.Close();
}
catch(Source.DataObjects.Referral.InvalidReferalDataException)
@ -154,5 +188,97 @@ namespace PWAPPv2
}
}
//AddImage Button
private void Button_Click_2(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "Image files (*.jpg,*.jpeg)|*.jpg;*.jpeg";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
if(openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
{
try
{
images.Add(new Source.DataObjects.PWImage(filename));
}
catch(NullReferenceException)
{
images = new List<Source.DataObjects.PWImage>();
images.Add(new Source.DataObjects.PWImage(filename));
}
}
}
try
{
ImageList.Items.Clear();
foreach (Source.DataObjects.PWImage image in images)
{
ImageList.Items.Add(CreateImageGridItem(image));
}
}
catch(NullReferenceException)
{ }
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
int index = ImageList.SelectedIndex;
if(index == -1)
{
return;
}
images.RemoveAt(index);
try
{
ImageList.Items.Clear();
foreach (Source.DataObjects.PWImage image in images)
{
ImageList.Items.Add(CreateImageGridItem(image));
}
}
catch (NullReferenceException)
{ }
}
public Grid CreateImageGridItem(Source.DataObjects.PWImage image)
{
Grid imageGrid = new Grid();
imageGrid.Width = 477;
imageGrid.HorizontalAlignment = HorizontalAlignment.Left;
imageGrid.VerticalAlignment = VerticalAlignment.Top;
imageGrid.ShowGridLines = true;
imageGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
ColumnDefinition imageColumn = new ColumnDefinition();
imageColumn.Width = new GridLength(50);
ColumnDefinition textColumn = new ColumnDefinition();
textColumn.Width = new GridLength(427);
imageGrid.ColumnDefinitions.Add(imageColumn);
imageGrid.ColumnDefinitions.Add(textColumn);
Image addedImage = new Image();
addedImage.Source = image.GetBitmapImage();
TextBlock text = new TextBlock();
text.Text = image.GetPath();
text.FontSize = 12;
Grid.SetColumn(addedImage, 0);
Grid.SetColumn(text, 1);
imageGrid.Children.Add(addedImage);
imageGrid.Children.Add(text);
return imageGrid;
}
}
}

View File

@ -210,14 +210,17 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="PwImage.cs" />
<Compile Include="Source\API\APIConfig.cs" />
<Compile Include="Source\API\APIConnection.cs" />
<Compile Include="Source\API\APIRequestBuilder.cs" />
<Compile Include="Source\Database\DatabaseConfig.cs" />
<Compile Include="Source\Database\DatabaseConnection.cs" />
<Compile Include="Source\DataObjects\APICredentials.cs" />
<Compile Include="Source\DataObjects\Attachment.cs" />
<Compile Include="Source\DataObjects\ComboBoxData.cs" />
<Compile Include="Source\DataObjects\Exceptions.cs" />
<Compile Include="Source\DataObjects\PWImage.cs" />
<Compile Include="Source\DataObjects\NumValList.cs" />
<Compile Include="Source\DataObjects\NumValPair.cs" />
<Compile Include="Source\DataObjects\ReferFromBox.cs" />

6
PWAPPv2/PwImage.cs Normal file
View File

@ -0,0 +1,6 @@
namespace PWAPPv2
{
internal class PwImage
{
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PWAPPv2.Source.DataObjects
{
class Attachment
{
APICredentials Credentials;
PWImage image;
string Token;
public Attachment(APICredentials credentials, PWImage pwImage, string token)
{
Credentials = credentials;
image = pwImage;
Token = token;
}
public string ToJsonString()
{
return "\"{" + Credentials.BuildJsonBodyContents() +
",'UserNum':'API'," +
"'AttToken':'" + Token.Replace("\"", "") + "'," +
"'FileName':'" + image.ShortFileName() + "'," +
"'ContentType':'_1'," +
"'ThumbExists':'1'," +
"'ZoomExists':'1'," +
"'FileDate':''," +
"'Base64FileContents':'" + image.GetBase64String() + "',\n" +
"'Base64ThumbContents':'" + image.GetBase64ThumbString() + "',\n" +
"'Base64ZoomContents':'" + image.GetBase64ZoomString() + "'}\"";
}
}
}

View File

@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Drawing;
using System.IO;
namespace PWAPPv2.Source.DataObjects
{
public class PWImage
{
private string ImagePath;
private BitmapImage bitmap;
//private Bitmap bmp;
//private Bitmap thumb;
//private Bitmap zoom;
public PWImage(string path)
{
ImagePath = path;
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@ImagePath);
bitmap.DecodePixelWidth = 100;
bitmap.EndInit();
//bmp = (Bitmap)System.Drawing.Image.FromFile(ImagePath);
//thumb = resize(bmp, 50, 50);
//zoom = resize(bmp, bmp.Width, bmp.Height);
}
public string GetPath()
{
return ImagePath;
}
public BitmapImage GetBitmapImage()
{
return bitmap;
}
public string GetBase64String()
{
using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
}
public string GetBase64ThumbString()
{
using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath))
{
Image thumb = resize(image, new Size(50, 50));
using (MemoryStream m = new MemoryStream())
{
thumb.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
}
public string GetBase64ZoomString()
{
using (System.Drawing.Image image = System.Drawing.Image.FromFile(ImagePath))
{
Image zoom = resize(image, new Size(image.Width + 800, image.Height + 800));
using (MemoryStream m = new MemoryStream())
{
zoom.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
return Convert.ToBase64String(imageBytes);
}
}
}
private Image resize(Image toResize, Size size)
{
return new Bitmap(toResize, size);
}
public string ShortFileName()
{
return ImagePath.Substring(ImagePath.LastIndexOf("\\") + 1);
}
}
}

View File

@ -20,7 +20,9 @@ namespace PWAPPv2.Source.DataObjects
RichTextBox RemarksBox;
CheckBox ContactBox;
public Referral(APICredentials apiCredentials, Patient patient, ReferralTypeBox referralType, ReferToBox referTo, ReferFromBox referFrom, RichTextBox remarksBox, CheckBox contactBox)
bool Attachments;
public Referral(APICredentials apiCredentials, Patient patient, ReferralTypeBox referralType, ReferToBox referTo, ReferFromBox referFrom, RichTextBox remarksBox, CheckBox contactBox, bool attachments)
{
ApiCredentials = apiCredentials;
PatientData = patient;
@ -29,6 +31,7 @@ namespace PWAPPv2.Source.DataObjects
ReferFrom = referFrom;
RemarksBox = remarksBox;
ContactBox = contactBox;
Attachments = attachments;
}
public string ToJsonString()
@ -42,6 +45,10 @@ namespace PWAPPv2.Source.DataObjects
contact = 1;
}
string attachmentsText = "False";
if (Attachments)
attachmentsText = "True";
try
{
return "\"{" + ApiCredentials.BuildJsonBodyContents() + "," +
@ -61,7 +68,7 @@ namespace PWAPPv2.Source.DataObjects
"'SubmittedBy':'" + ReferFrom.GetSelectedID() + "'," +
"'ToPracticeId':'" + ReferTo.GetSelectedPracticeID() + "'," +
"'ToDoctorID':'" + ReferTo.GetSelectedDoctorID() + "'," +
"'Attachments':'False'," +
"'Attachments':'" + attachmentsText + "'," +
"'Remarks':'" + RemarksText + "'," +
"'Contact':'" + contact + "'}\"";
}