public
static
void
CompressImgByte
(
int
dWidth
,
int
dHeight
,
Stream
inputStream
,
Stream
outStream
,
int
flag
=
80
,
string
arg
=
null
)
inputStream
.
Position
=
0
;
System
.
Drawing
.
Image
iSource
=
System
.
Drawing
.
Image
.
FromStream
(
inputStream
)
;
var
prop
=
iSource
.
PropertyItems
.
FirstOrDefault
(
x
=>
x
.
Id
==
274
)
;
if
(
prop
!=
null
)
byte
[
]
buffed
=
prop
.
Value
;
StringBuilder
sbv
=
new
StringBuilder
(
)
;
foreach
(
var
byteValue
in
buffed
)
sbv
.
Append
(
byteValue
.
ToString
(
"x2"
)
)
;
string
value2
=
sbv
.
ToString
(
)
;
if
(
value2
.
Equals
(
"0006"
)
||
value2
.
Equals
(
"0600"
)
)
iSource
.
RotateFlip
(
RotateFlipType
.
Rotate90FlipNone
)
;
System
.
Drawing
.
Imaging
.
ImageFormat
tFormat
=
iSource
.
RawFormat
;
if
(
dWidth
>
0
&&
iSource
.
Width
>
dWidth
&&
iSource
.
Width
>
iSource
.
Height
)
dHeight
=
dWidth
*
iSource
.
Height
/
iSource
.
Width
;
else
if
(
dWidth
>
0
&&
dHeight
==
0
&&
iSource
.
Width
>
dWidth
)
dHeight
=
dWidth
*
iSource
.
Height
/
iSource
.
Width
;
else
if
(
dWidth
==
0
&&
dHeight
>
0
&&
iSource
.
Width
>
iSource
.
Height
)
dWidth
=
dHeight
*
iSource
.
Width
/
iSource
.
Height
;
else
if
(
dHeight
>
iSource
.
Height
||
dWidth
>
iSource
.
Width
)
dWidth
=
iSource
.
Width
;
dHeight
=
iSource
.
Height
;
else
if
(
dHeight
>
0
&&
iSource
.
Width
<
iSource
.
Height
)
dWidth
=
dHeight
*
iSource
.
Width
/
iSource
.
Height
;
dWidth
=
iSource
.
Width
;
dHeight
=
iSource
.
Height
;
Bitmap
ob
=
new
Bitmap
(
dWidth
,
dHeight
)
;
Graphics
g
=
Graphics
.
FromImage
(
ob
)
;
g
.
Clear
(
System
.
Drawing
.
Color
.
Transparent
)
;
g
.
CompositingQuality
=
System
.
Drawing
.
Drawing2D
.
CompositingQuality
.
HighQuality
;
g
.
SmoothingMode
=
System
.
Drawing
.
Drawing2D
.
SmoothingMode
.
HighQuality
;
g
.
InterpolationMode
=
System
.
Drawing
.
Drawing2D
.
InterpolationMode
.
HighQualityBicubic
;
g
.
DrawImage
(
iSource
,
new
Rectangle
(
0
,
0
,
dWidth
,
dHeight
)
,
0
,
0
,
iSource
.
Width
,
iSource
.
Height
,
GraphicsUnit
.
Pixel
)
;
g
.
Dispose
(
)
;
System
.
Drawing
.
Imaging
.
EncoderParameters
ep
=
new
System
.
Drawing
.
Imaging
.
EncoderParameters
(
)
;
long
[
]
qy
=
new
long
[
1
]
;
qy
[
0
]
=
flag
;
System
.
Drawing
.
Imaging
.
EncoderParameter
eParam
=
new
System
.
Drawing
.
Imaging
.
EncoderParameter
(
System
.
Drawing
.
Imaging
.
Encoder
.
Quality
,
qy
)
;
ep
.
Param
[
0
]
=
eParam
;
System
.
Drawing
.
Imaging
.
ImageCodecInfo
[
]
arrayICI
=
System
.
Drawing
.
Imaging
.
ImageCodecInfo
.
GetImageEncoders
(
)
;
System
.
Drawing
.
Imaging
.
ImageCodecInfo
jpegICIinfo
=
arrayICI
.
FirstOrDefault
(
x
=>
x
.
FormatDescription
.
Equals
(
"JPEG"
)
)
;
if
(
jpegICIinfo
!=
null
)
ob
.
Save
(
outStream
,
jpegICIinfo
,
ep
)
;
ob
.
Save
(
outStream
,
tFormat
)
;
catch
(
Exception
ex
)
string
msg
=
ex
.
Message
;
Console
.
WriteLine
(
msg
)
;
finally
iSource
.
Dispose
(
)
;
ob
.
Dispose
(
)
;
public
static
void
CompressImgFile
(
int
dWidth
,
int
dHeight
,
string
filePath
,
string
outFilePath
,
int
flag
=
80
)
System
.
Drawing
.
Image
iSource
=
System
.
Drawing
.
Image
.
FromFile
(
filePath
)
;
foreach
(
var
item
in
iSource
.
PropertyItems
)
if
(
item
.
Id
==
274
)
byte
[
]
buffed
=
item
.
Value
;
StringBuilder
sbv
=
new
StringBuilder
(
)
;
foreach
(
var
byteValue
in
buffed
)
sbv
.
Append
(
byteValue
.
ToString
(
"x2"
)
)
;
string
value2
=
sbv
.
ToString
(
)
;
System
.
Diagnostics
.
Debug
.
WriteLine
(
"方向="
+
value2
)
;
if
(
value2
.
Equals
(
"0006"
)
||
value2
.
Equals
(
"0600"
)
)
iSource
.
RotateFlip
(
RotateFlipType
.
Rotate90FlipNone
)
;
break
;
System
.
Drawing
.
Imaging
.
ImageFormat
tFormat
=
iSource
.
RawFormat
;
if
(
dWidth
>
0
&&
iSource
.
Width
>
dWidth
&&
iSource
.
Width
>
iSource
.
Height
)
dHeight
=
dWidth
*
iSource
.
Height
/
iSource
.
Width
;
else
if
(
dWidth
>
0
&&
dHeight
==
0
&&
iSource
.
Width
>
dWidth
)
dHeight
=
dWidth
*
iSource
.
Height
/
iSource
.
Width
;
else
if
(
dWidth
==
0
&&
dHeight
>
0
&&
iSource
.
Width
>
iSource
.
Height
)
dWidth
=
dHeight
*
iSource
.
Width
/
iSource
.
Height
;
else
if
(
dHeight
>
iSource
.
Height
||
dWidth
>
iSource
.
Width
)
dWidth
=
iSource
.
Width
;
dHeight
=
iSource
.
Height
;
else
if
(
dHeight
>
0
&&
iSource
.
Width
<
iSource
.
Height
)
dWidth
=
dHeight
*
iSource
.
Width
/
iSource
.
Height
;
dWidth
=
iSource
.
Width
;
dHeight
=
iSource
.
Height
;
Bitmap
ob
=
new
Bitmap
(
dWidth
,
dHeight
)
;
Graphics
g
=
Graphics
.
FromImage
(
ob
)
;
g
.
Clear
(
System
.
Drawing
.
Color
.
Transparent
)
;
g
.
CompositingQuality
=
System
.
Drawing
.
Drawing2D
.
CompositingQuality
.
HighQuality
;
g
.
SmoothingMode
=
System
.
Drawing
.
Drawing2D
.
SmoothingMode
.
HighQuality
;
g
.
InterpolationMode
=
System
.
Drawing
.
Drawing2D
.
InterpolationMode
.
HighQualityBicubic
;
g
.
DrawImage
(
iSource
,
new
Rectangle
(
0
,
0
,
dWidth
,
dHeight
)
,
0
,
0
,
iSource
.
Width
,
iSource
.
Height
,
GraphicsUnit
.
Pixel
)
;
g
.
Dispose
(
)
;
System
.
Drawing
.
Imaging
.
EncoderParameters
ep
=
new
System
.
Drawing
.
Imaging
.
EncoderParameters
(
)
;
long
[
]
qy
=
new
long
[
1
]
;
qy
[
0
]
=
flag
;
System
.
Drawing
.
Imaging
.
EncoderParameter
eParam
=
new
System
.
Drawing
.
Imaging
.
EncoderParameter
(
System
.
Drawing
.
Imaging
.
Encoder
.
Quality
,
qy
)
;
ep
.
Param
[
0
]
=
eParam
;
System
.
Drawing
.
Imaging
.
ImageCodecInfo
[
]
arrayICI
=
System
.
Drawing
.
Imaging
.
ImageCodecInfo
.
GetImageEncoders
(
)
;
System
.
Drawing
.
Imaging
.
ImageCodecInfo
jpegICIinfo
=
null
;
string
imgExtend
=
"JPEG"
;
for
(
int
x
=
0
;
x
<
arrayICI
.
Length
;
x
++
)
if
(
arrayICI
[
x
]
.
FormatDescription
.
Equals
(
imgExtend
)
)
jpegICIinfo
=
arrayICI
[
x
]
;
break
;
if
(
jpegICIinfo
!=
null
)
ob
.
Save
(
outFilePath
,
jpegICIinfo
,
ep
)
;
ob
.
Save
(
outFilePath
,
tFormat
)
;
catch
(
Exception
ex
)
string
msg
=
ex
.
Message
;
Console
.
WriteLine
(
msg
)
;
finally
iSource
.
Dispose
(
)
;
ob
.
Dispose
(
)
;
string fileInput = "E:\\测试图片\\2023_11_07_155548.3179.jpg";
string outFile = "E:\\测试图片\\2023_11_07_155548.3179_c2222.jpg";
var fileByter= File.ReadAllBytes(fileInput);
System.IO.Stream inStream = new System.IO.MemoryStream();
inStream.Write(fileByter, 0, fileByter.Length);
System.IO.Stream outStream = new System.IO.MemoryStream();
ImageHandle.CompressImgByte(1300, 1300, inStream, outStream);
outStream.Position = 0;
byte[] fileYaSuoByte = new byte[outStream.Length];
outStream.Read(fileYaSuoByte, 0, (int)outStream.Length);
File.WriteAllBytes(outFile,fileYaSuoByte);
说明:该类也来源于网上,只不过我对其进行了一些改动。原类生成的缩略图的问题在于:高或者宽可能会大于需求值。比如,我需要的图片宽为150,高为120,当原图
* 高为150,宽为150或差距不大时,生成的图片不会有变化,即也是高150,宽150,这样,当把这些图片显示到网页上的高为150,高为120的规格里的时候,高方向就会
* 被撑破 。
* 修改后:当出现这种情况后,图片会按最小边进行缩略,即如果原图高为150,宽为150,要求宽150,高120,那生成的图片就是120*120。总之,就是不会出现网页被
* 撑破的或变形的情况。
* 使用方法:
* zoomImage.MakeZoomImage(原文件名, 缩略图文件名, 缩略图宽,缩略图高 , 模式);
* 如:zoomImage.MakeZoomImage(Server.MapPath("~/uploads/") + bigFilename, Server.MapPath("~/uploads/") + smallFileName1, 150, 120, "HW");
//将图片保存在内存中
Emgu.CV.Util.VectorOfByte buf = new Emgu.CV.Util.VectorOfByte();
CvInvoke.Imencode(".png", mat, buf);
byte[] vs = buf.ToArray
根据一个Image的Url链接可以在浏览器中显示一个图片,如果要通过代码将图片保存在本地磁盘可以通过以下方式:
1、首先获取图片的二进制数组。
static public byte[] GetBytesFromUrl(string url)
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebReques...
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "text/plain";
//接受用户上传
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果)
代码如下:
using System;using System.Drawing;
using System.Drawing.Drawi
参数解答:
1 源图路径:是指你那张图的物理路径,如:@"E:\2001\1.jpg";
2 缩略图路径: 是指图片已生成缩略图后应放的路径,如:@"E:2002\1_min.jpg";
3 缩略图宽度:是指想要生成的缩略图的宽度,...
背景:在地图瓦片的请求过程中,可能存在瓦片遗失的情况,这时候需要在地图上展示默认的瓦片图片,之前在前端相关地图包中去实现,现在在后台实现当请求瓦片失败时,返回默认的瓦片图片,且支持默认图片配置。
1.创建.net 5 Web项目
2.引入Microsoft.Extensions.Configuration,用于读取appsettings.json
3.新建中间件类DefaultImageMiddleware
public class DefaultImageMiddleware
tortoisegit常见错误Disconnected no supported authentication methods available(server sent: publickey)
49862
Cannot write DateTime with Kind=Unspecified to PostgreSQL type ‘timestamp with time zone‘, only UTC
Linux中.NET读取excel组件,不会出现The type initializer for ‘Gdip‘ threw an exception异常