C# 弹出窗口查看图片以及上传图片

时间:2022-05-17 17:09:14
  private void ShowSelectedPicture(string path)
{
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = ;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource =new MemoryStream(image );
bitmapImage.EndInit();
var pictureWindow = new PictureWindow();//自己创建的窗口
pictureWindow.myImage.Source = bitmapImage;//myImage窗口中的图片空间
//pictureWindow.myImage.Width = bitmapImage.PixelWidth;
//pictureWindow.myImage.Height = bitmapImage.PixelHeight;
pictureWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
pictureWindow.ShowDialog();
}

private void MyImage_OnMouseWheel(object sender, MouseWheelEventArgs e)
    {
         double ScaleX = 0;
         double ScaleY = 0;
         double dbl_ZoomX = ((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).ScaleX;
         double dbl_ZoomY = ((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).ScaleY;
        ((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).CenterX = e.GetPosition(this.myImage).X;
        ((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).CenterY = e.GetPosition(this.myImage).Y;


if (e.Delta < 0)
       {
          ScaleX = dbl_ZoomX - 0.1 < 0.2 ? 0.1 : dbl_ZoomX - 0.1;
          ScaleY = dbl_ZoomY - 0.1 < 0.2 ? 0.1 : dbl_ZoomY - 0.1;
       }
     else if (e.Delta > 0)
     {
        ScaleX = dbl_ZoomX + 0.1 > 10.0 ? 10.0 : dbl_ZoomX + 0.1;
        ScaleY = dbl_ZoomY + 0.1 > 10.0 ? 10.0 : dbl_ZoomY + 0.1;
     }


((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).ScaleX = ScaleX;
       ((ScaleTransform)(((TransformGroup)(((UIElement)(this.myImage)).RenderTransform)).Children[0])).ScaleY = ScaleY;
    }

 

C# 上传图片

 private void UploadImage(string Path)
{
FileStream fullfs;
string pictureName = GetPictureName();
string pictureFullPath = storePath;// GetPicturePath(rtdto.BusinessIndex, rtdto.ProviderIndex);
string pictureFullName = pictureFullPath + @"\" + pictureName;
fullfs = new FileStream(pictureFullName, FileMode.Create);
BinaryWriter fullbw = new BinaryWriter(fullfs);
fullbw.Write(pidto.PictureData);
fullbw.Close();
fullfs.Close();
}
private string GetPicturePath(int businessindex, int providerindex)
{
string currentPath = AppDomain.CurrentDomain.BaseDirectory + @"\Image";
if (!System.IO.Directory.Exists(currentPath))
{
try
{
System.IO.Directory.CreateDirectory(currentPath);
}
catch
{
currentPath = AppDomain.CurrentDomain.BaseDirectory;//创建目录失败,存入根目录中
}
}
string filePath = currentPath + @"\" + businessindex + "_" + providerindex;
if (!System.IO.Directory.Exists(filePath))
{
try
{
System.IO.Directory.CreateDirectory(filePath);
}
catch
{
filePath = currentPath;//创建目录失败,存入根目录中
} }
return filePath;
}
private string GetPictureName()
{
string imageGuid = Guid.NewGuid().ToString() + ".jpg";
return imageGuid;
}

窗口页面代码

<Window x:Class="App.Modules.PictureWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uiControls="clr-namespace:Allegion.Components.UIControls;assembly=Allegion.Components.UIControls"
Title="PictureWindow"> <ScrollViewer x:Name="scrollViewer"
HorizontalScrollBarVisibility="Auto"
MouseWheel="ScrollViewer_OnMouseWheel"
VerticalScrollBarVisibility="Auto"> <Image Name="myImage" MouseWheel="MyImage_OnMouseWheel">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="" ScaleY="" />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Image.RenderTransform>
</Image>
</ScrollViewer>
</Window>