我正在开发一个程序,将URL中的图像下载到bitmapimage并显示它.接下来我尝试使用jpegbitmapencoder将bitmapimage保存到硬盘驱动器.文件已成功创建,但实际的jpeg图像为空或1个黑色像素.
public Guid SavePhoto(string istrImagePath)
{
ImagePath = istrImagePath;
BitmapImage objImage = new BitmapImage(
new Uri(istrImagePath, UriKind.RelativeOrAbsolute));
PictureDisplayed.Source = objImage;
savedCreationObject = objImage;
Guid photoID = System.Guid.NewGuid();
string photolocation = photoID.ToString() + ".jpg"; //file name
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(objImage));
using (FileStream filestream = new FileStream(photolocation, FileMode.Create))
{
encoder.Save(filestream);
}
return photoID;
}
Run Code Online (Sandbox Code Playgroud)
这是保存和显示照片的功能.照片显示正确,但保存时再次显示我得到一个空的jpeg或1个黑色像素.
在底部看到我的答案
只是在WPF上做一些轻量级阅读,我需要从DataGrid绑定selectedItems,但我无法想出任何有形的东西.我只需要选定的对象.
数据网格:
<DataGrid Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="4"
Name="ui_dtgAgreementDocuments"
ItemsSource="{Binding Path=Documents, Mode=TwoWay}"
SelectedItem="{Binding Path=DocumentSelection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="White"
SelectionMode="Extended" Margin="2,5"
IsReadOnly="True"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeRows="False"
GridLinesVisibility="None"
HorizontalScrollBarVisibility="Hidden"
columnHeaderStyle="{StaticResource GreenTea}"
HeadersVisibility="Column"
BorderThickness="2"
BorderBrush="LightGray"
CellStyle="{StaticResource NonSelectableDataGridCellStyle}"
SelectionUnit="FullRow"
HorizontalContentAlignment="Stretch" AutoGenerateColumns="False">
Run Code Online (Sandbox Code Playgroud) 我正在使用ListView
控件来显示一些数据行.有一个后台任务接收列表内容的外部更新.新接收的数据可能包含更少,更多或相同数量的项目,并且项目本身也可能已更改.
在ListView.ItemsSource
绑定到OberservableCollection
(_itemList),以便更改_itemList应在也可以看到ListView
.
_itemList = new ObservableCollection<PmemCombItem>();
_itemList.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
L_PmemCombList.ItemsSource = _itemList;
Run Code Online (Sandbox Code Playgroud)
为了避免刷新完整的ListView,我将新检索的列表与当前_itemList进行简单比较,更改不相同的项目,并在必要时添加/删除项目.集合"newList"包含新创建的对象,因此替换_itemList中的项目正确地发送"刷新"通知(我可以使用OnCollectionChanged
ObservableCollection 的事件处理程序进行记录)
Action action = () =>
{
for (int i = 0; i < newList.Count; i++)
{
// item exists in old list -> replace if changed
if (i < _itemList.Count)
{
if (!_itemList[i].SameDataAs(newList[i]))
_itemList[i] = newList[i];
}
// new list contains more items -> add items
else
_itemList.Add(newList[i]);
}
// new list contains less items …
Run Code Online (Sandbox Code Playgroud) 我有一个byte[]
代表图像的原始数据.我想把它转换为BitmapImage
.
我尝试了几个我发现的例子但是我一直得到以下异常
"没有找到适合完成此操作的成像组件."
我认为这是因为我byte[]
实际上并不代表图像而只是原始位.所以我的问题如上所述是如何将原始位的byte []转换为a BitmapImage
.
输入命中测试在其RenderTransform
属性中具有大缩放因子的Path元素上产生不正确的结果.
以下XAML定义了带有实心圆和Hand
光标的路径.
<Canvas Background="LightGray">
<Path StrokeThickness="0" Fill="Blue" Cursor="Hand">
<Path.Data>
<EllipseGeometry RadiusX=".5" RadiusY=".5" Center="1,1"/>
</Path.Data>
<Path.RenderTransform>
<ScaleTransform ScaleX="150" ScaleY="150"/>
</Path.RenderTransform>
</Path>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
如下图所示,Hand
尽管光标的位置在形状之外,但仍会出现光标.
使用较大的Path和较小的缩放因子,问题消失,并且Cursor按预期运行.
<Canvas Background="LightGray">
<Path StrokeThickness="0" Fill="Blue" Cursor="Hand">
<Path.Data>
<EllipseGeometry RadiusX="50" RadiusY="50" Center="100,100"/>
</Path.Data>
<Path.RenderTransform>
<ScaleTransform ScaleX="1.5" ScaleY="1.5"/>
</Path.RenderTransform>
</Path>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
像这样执行明确的命中测试
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var canvas = (UIElement)sender;
var hitElement = canvas.InputHitTest(e.GetPosition(canvas));
Trace.TraceInformation("hitElement = {0}", hitElement);
}
Run Code Online (Sandbox Code Playgroud)
在Canvas上的鼠标事件处理程序中给出了相同的错误结果.在缩放路径之外清楚地点击鼠标仍然会将Path作为命中元素返回.
还值得注意的是,Silverlight中没有出现此问题.
现在的问题是:这种行为的原因是什么,如何避免?请注意,我不能简单地更改Path元素的原始大小,因此像"不使用大比例因子"这样的答案将没有用.
我目前的解决方法不是通过RenderTransform转换Path,而是转换Data(通过将转换应用于Geometry.Transform
属性).但由于可能存在复杂的填充(例如,使用ImageBrush),我也必须转换填充画笔(这不仅涉及设置其变换,还涉及它们的视口).
此外,实际变换不仅是缩放,而且还是一个也可以旋转和翻译的MatrixTransform.
值得注意的是,该问题也出现在其他几何形状和其他变换中.例如,带有RectangleGeometry的转换路径显示类似的错误行为.
大比例因子不正确:
<Canvas Background="LightGray"> …
Run Code Online (Sandbox Code Playgroud) 我有创建xaml控件的问题.我正在使用通用应用程序在VS 2015中编写新项目.我想创建网格.在这个网格中我想要一个按钮.在模型中,我指定了列(Level)和Row.这是我的代码:
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=TechnologyList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="14*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="Control">
<Setter Property="Grid.Column" Value="{Binding Level}" />
<Setter Property="Grid.Row" Value="{Binding Row}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我遇到错误<Setter Property="Grid.Column" Value="{Binding Level}" />
错误:HRESULT的异常:0x8000FFFF(E_UNEXPECTED)在edytor中没有运行代码.怎么了?在"旧"WPF中一切都很好,但在Universal …
如何让CheckBox为空?我只需要打勾.现在它需要额外的空白空间,就像这里:
<CheckBox
Background="Aqua"
Margin="0,0,0,0"/>
Run Code Online (Sandbox Code Playgroud)
(我添加了颜色以查看此控件占用多少空间(空白空间导致所有问题)).
我只需要刻度矩形,我不想要空的空间.我怎样才能做到这一点?
将边距设置为零并将内容设置为""不起作用.
我需要将选定的 .svg 文件转换为 System.Drawing.Image 对象,以便我可以调整它的大小并将其另存为 .png。谁能帮我这个?
这是我到目前为止所拥有的:
Svg.SvgDocument svgDocument = SVGParser.GetSvgDocument(mPath);
image = svgDocument.Draw();
Run Code Online (Sandbox Code Playgroud)
但它给了我内存不足的错误。
我试图将代码从控制台应用程序转移到uwp,在这个使用ServicePointManager的控制台应用程序中使用所有证书,在uwp应用程序中我不使用它们并且文本有异常 - "无法建立与服务器的连接".
问题是 - 如何替换ServicePointManager并在uwp中应用所有证书(因为我不知道该服务器使用的证书类型).
CodePointManager代码如下:
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback
(bypassAllCertificateStuff);
private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的答复!
我有一个简单的应用程序,包含一个包含Canvas(rootCanvas)的窗口.我试图添加另一个Canvas(测试)并将不同的变换应用于子画布的LayoutTransform.这都是以编程方式完成的,而不是使用XAML.
有些转型正在发挥作用,而其他转型则不如下:
代码如下:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Canvas rootCanvas = (Canvas)Application.Current.Windows[0].FindName("canvas1");
Canvas test = new Canvas();
test.Width = 10;
test.Height = 10;
test.Background = Brushes.Aqua;
// this works
//test.LayoutTransform = new RotateTransform(45);
// this doesn't
//test.LayoutTransform = new TranslateTransform(40, 40);
// only the rotate part of this works
Matrix matrix = new Matrix();
matrix.Rotate(45);
matrix.Translate(40, 40);
test.LayoutTransform = new MatrixTransform(matrix);
rootCanvas.Children.Add(test);
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人能解释我在这里做错了什么,我将非常感激,因为我不明白为什么翻译似乎没有像我期望的那样起作用.
提前致谢,
Wibbs