这个问题是关于如何读/写,分配和管理位图的像素数据.
下面是一个如何为像素数据分配字节数组(托管内存)并使用它创建位图的示例:
Size size = new Size(800, 600);
PixelFormat pxFormat = PixelFormat.Format8bppIndexed;
//Get the stride, in this case it will have the same length of the width.
//Because the image Pixel format is 1 Byte/pixel.
//Usually stride = "ByterPerPixel"*Width
Run Code Online (Sandbox Code Playgroud)
//但并非总是如此.更多信息在bobpowell.
int stride = GetStride(size.Width, pxFormat);
byte[] data = new byte[stride * size.Height];
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
Bitmap bmp = new Bitmap(size.Width, size.Height, stride,
pxFormat, handle.AddrOfPinnedObject());
//After doing your stuff, free the Bitmap and unpin the array.
bmp.Dispose(); …Run Code Online (Sandbox Code Playgroud) Visual Studio 2013大括号匹配,引用突出显示,自动检测变量/,方法名称更改(需要使用Refactor)等...正在停止工作并且仅在VS重新启动后再次工作.我正在用C#编写代码.
我是这个问题的唯一受苦/受影响吗?
很讨厌VS2013的问题!! 它接缝只发生在大型项目中.
更新1:我意识到它发生在我在VS Designer中打开任何WinForm之后.当我回到代码编辑时,大括号匹配和东西都消失了,我需要重启VS!
更新2:Visual Studio 2013 Update 4仍然......没有修复!你好微软?
更新3:由于我的解决方案投票很少,我将在此总结一下.在我的例子中,问题是由VS Designer调用的Thread.Sleep.这是一个代码错误,但无论如何,Designer不应该运行Thread.Sleep命令冻结整个VS.
我有这个文件类型过滤器:
public const string Png = "PNG Portable Network Graphics (*.png)|" + "*.png";
public const string Jpg = "JPEG File Interchange Format (*.jpg *.jpeg *jfif)|" + "*.jpg;*.jpeg;*.jfif";
public const string Bmp = "BMP Windows Bitmap (*.bmp)|" + "*.bmp";
public const string Tif = "TIF Tagged Imaged File Format (*.tif *.tiff)|" + "*.tif;*.tiff";
public const string Gif = "GIF Graphics Interchange Format (*.gif)|" + "*.gif";
public const string AllImages = "Image file|" + "*.png; *.jpg; *.jpeg; *.jfif; *.bmp;*.tif; *.tiff; *.gif";
public …Run Code Online (Sandbox Code Playgroud) 在我的DLL中有一个我想要导出的方法.
//作品:
extern "C" __declspec(dllexport)
Run Code Online (Sandbox Code Playgroud)
//不会工作
__declspec(dllexport)
Run Code Online (Sandbox Code Playgroud)
C++导出:
extern "C" __declspec(dllexport) int Test();
Run Code Online (Sandbox Code Playgroud)
C#import:
[DllImport("CircleGPU2_32.DLL", EntryPoint = "Test",
CallingConvention = CallingConvention.StdCall)]
public static extern int Test();
Run Code Online (Sandbox Code Playgroud)
为什么我需要外部"C"?
IDE上的向后导航向后(Ctrl + - )和向前(Ctrl + SHIFT + - )按钮用于由VS 2010中的鼠标横向后退/前进按钮控制.
在VS 2012中,鼠标按钮仅在某些情况下有效,例如在方法调用中单击"转到定义"后返回.它们未连接到IDE按钮.
试图解决这个问题,我已经安装了这个插件:鼠标导航
好吧,它运行正常,直到你使用"转到定义",之后,嵌入式VS后退/前进鼠标功能开始与插件冲突,导致凌乱的行为!
我是唯一有这个问题的人吗?我试过谷歌,但我什么都没发现.
这真烦人!我希望有人可以帮我解决这个问题.
编辑:
正如您在标记的答案中看到的那样,该插件已由其开发人员修复,这要归功于此主题.最后一个版本(2.2.0)工作正常.
一些澄清:
Visual Studio(2012)将正常导航与"点击进入"导航分开.
正常导航:鼠标单击代码编辑器中的任意位置,也可以更改标签
点击导航:转到定义,搜索结果点击.它由名为"Browse Back/Next""Previous/Next Definition,Declaration or Reference"的按钮控制.您可以在"查看自定义"工具栏中找到它.
默认的VS鼠标后退/前进按钮附加到"浏览后退/下一步"按钮,而不是导航后退/前进.
VS插件鼠标导航将鼠标按钮设置为向后/向前导航.唯一剩下的问题是,因为VS不会将"click to go"视为正常导航,当您单击转到定义并尝试使用Ctrl + - 或鼠标返回时,您将不会返回到最后位置,但到以前的"正常导航"位置.
我对插件开发人员的建议是尝试让VS认为"点击进入"导航为正常导航,完全解决了这个问题.
有没有办法可以用readonly值创建静态数组,但是使用一些逻辑来创建它?让我试着解释一下:
我知道我可以这样做:
public static readonly int[] myArray = { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
但是可以这样做:
public static readonly int[] myArray2 =
{
for (int i = 0; i < 256; i++)
{
float[i] = i;
}
};
Run Code Online (Sandbox Code Playgroud)
编辑:我的问题的一个很好的解决方案:静态构造函数!http://msdn.microsoft.com/en-us/library/k9x6w0hc%28v=VS.100%29.aspx:d
我需要阻止特定的绘图条目显示在Matlab绘图图例上.
样品:
% x and y are any plot data
for i=1:5
plot(x,y);
plot(x2,y2,'PleaseNoLegend!'); % I need to hide this from legend
end
legend('show');
Run Code Online (Sandbox Code Playgroud)
我可以在plot命令中设置任何标志,这样这个特定条目不会出现在图例中吗?
我正在寻找一种简单的方法来创建一个受锥形区域约束的随机单位向量.原点始终是[0,0,0].
我的解决方案到目前为止:
function v = GetRandomVectorInsideCone(coneDir,coneAngleDegree)
coneDir = normc(coneDir);
ang = coneAngleDegree + 1;
while ang > coneAngleDegree
v = [randn(1); randn(1); randn(1)];
v = v + coneDir;
v = normc(v);
ang = atan2(norm(cross(v,coneDir)), dot(v,coneDir))*180/pi;
end
Run Code Online (Sandbox Code Playgroud)
我的代码循环,直到随机生成的单位向量在定义的锥体内.有没有更好的方法呢?
使用Ahmed Fasih代码进行频率分配(在评论中). 我想知道如何获得矩形或正态分布.
c = [1;1;1]; angs = arrayfun(@(i) subspace(c, GetRandomVectorInsideCone(c, 30)), 1:1e5) * 180/pi; figure(); hist(angs, 50);
Run Code Online (Sandbox Code Playgroud)
测试代码:
clearvars; clc; close all;
coneDir = [randn(1); randn(1); randn(1)];
coneDir = [0 0 1]';
coneDir = normc(coneDir);
coneAngle = 45; …Run Code Online (Sandbox Code Playgroud) 如何为特定GUI轴设置Matlab GUI数据光标回调?我能够在GUI工具栏中添加数据光标图标.我可以在绘制的数据上选择3D点,但我需要在数据光标文本中添加更多信息,并使用它获得的坐标做一些其他的事情.
我试图遵循" 如何向数据光标添加其他信息? ",但不适用于GUI.
GUI轴不是数字,所以我收到这个错误:
Error using datacursormode (line 149)
Invalid figure handle
我在这里需要相同的,但对于GUI轴(数字?):
function test_main
% Plots graph and sets up a custom data tip update function
fig = figure('DeleteFcn','doc datacursormode');
X = 0:60;
t = (X)*0.02;
Y = sin(-16*t);
plot(X,Y)
dcm_obj = datacursormode(fig); % tried here "handles.MyFigHandle"
set(dcm_obj,'UpdateFcn',{@myupdatefcn,t})
function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['I: ',num2str(I)],...
['T: ',num2str(t(I))]}; …Run Code Online (Sandbox Code Playgroud) 我注意到在OnPaint事件上的图形转换比例之后,不会绘制图像的第一个像素列的一半.
重现它所需的所有代码都在帖子的末尾.基本上我已经创建了一个派生自PictureBox的类,名为PictureBox2,它会覆盖OnPaint方法来执行Scale转换.它还将InterpolationMode更改为NearestNeighbor以防止Graphics更改像素外观.
PictureBox控件已添加到名为Form6_GraphicsTest的Form中.控制在所有方面都是固定的.PictureBox2背面颜色更改为蓝色,Form返回颜色为深灰色.
正如您在下面的图像中看到的那样,只绘制了图像第一个像素列的1/2.为什么??我在这里遗漏了什么?

这是原始的10x10 8bpp图像:

编辑-解决方案 对于某些ODD原因PixelOffsetMode.Default吃掉0.5个像素.解决方案:PixelOffsetMode.Half或HighQuality!
代码PictureBox2.cs
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace GraphicsTest
{
public class PictureBox2 : PictureBox
{
public PointF Zoom = new PointF(20, 20);
private InterpolationMode interpolationMode = InterpolationMode.NearestNeighbor;
/// <summary>
/// Paint the image
/// </summary>
/// <param name="e">The paint event</param>
protected override void OnPaint(PaintEventArgs e)
{
if (IsDisposed)
return;
if (Image != null)
{
if (e.Graphics.InterpolationMode != interpolationMode)
e.Graphics.InterpolationMode = interpolationMode;
using (Matrix transform = e.Graphics.Transform)
{
//e.Graphics.ResetTransform(); …Run Code Online (Sandbox Code Playgroud) c# ×7
matlab ×3
.net ×1
arrays ×1
bitmap ×1
bitmapdata ×1
c++ ×1
dllexport ×1
filefilter ×1
graphics ×1
image ×1
intellisense ×1
matlab-guide ×1
plot ×1
plugins ×1
static ×1
vector ×1